datetime格式中日期数据的累计计数

q8l4jmvw  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(309)

我有一张table看起来像:

ID entry_time
abc123 2020-05-29 10:29:18.000
def456 2020-05-30 13:12:43.000
...

我想按日期进行累计计数,所以我做了: select entry_time, count (*) OVER (ORDER BY entry_time) as TOTAL_NUM from my_table; 没关系,但要根据实际情况来计算 datetime 格式。我只想指望 date (即白天,不在乎时间)。
我该怎么做?
非常感谢,

col17t5w

col17t5w1#

如果您希望每天有一条记录,可以使用聚合和窗口函数:

select
    date(entry_time) entry_day,
    sum(count(*)) over(order by date(entry_time)) total_num
from mytable
group by date(entry_time)
order by entry_day

这假设您的数据库支持 date() ,将 datetimedate (例如mysql)。如果它不这样做,它肯定有另一种方法来做到这一点。

fv2wmkja

fv2wmkja2#

你可以通过转换 entry time 迄今为止。

select 
    convert(date, entry_time) as entry_time, 
    count (*) OVER (ORDER BY convert(date, entry_time)) as total_num 
from my_table;
r1wp621o

r1wp621o3#

您可以使用转换或强制转换输入时间到日期

select entry_time, count (*) OVER (ORDER BY Convert(date,entry_time)) as TOTAL_NUM from my_table;

OR

select entry_time, count (*) OVER (ORDER BY Cast(entry_timeas as date)) as TOTAL_NUM from my_table;

相关问题