1

如何按自定义小时的日期时间列分组,例如每 5 小时或每 12 小时或每 24 小时。

Select CreateOn, Count(*) From Table1
Group By ?????
4

2 回答 2

2

您应该选择开始日期/时间,在此示例中为“2011-01-01”,然后使用DATEDIFF() 函数获取开始日期和CreateOn. 然后使用/运算符获取整数间隔数(本例中为 5 小时)。

Select 
min(DATEADD(HOUR,DATEDIFF (HOUR,'2011-01-01',CreateOn )/5*5,'2011-01-01')) 
   as Start_time,
max(DATEADD(HOUR,(DATEDIFF (HOUR,'2011-01-01',CreateOn )/5+1)*5,'2011-01-01')) 
   as End_Time,
DATEDIFF (HOUR,'2011-01-01',CreateOn )/5 as Interval_Number, 
Count(*) as _Count
From Table1
Group By DATEDIFF (HOUR,'2011-01-01',CreateOn )/5
于 2013-01-05T07:25:48.347 回答
1

如果我正确理解你的问题,你可以尝试这样的事情:

SELECT CreateOn, Count(*) FROM Table1 GROUP BY (DATEPART(MINUTE, [Date]) % 12)
于 2013-01-05T06:53:19.223 回答