我想逐个小时计数,但不能按小时分组。
select t_date_time_issued,t_street_name,count(t_reference) from tickets
WHERE t_date_time_issued BETWEEN '03/06/2015' AND '03/07/2015' and t_street_name like '%airport%'
GROUP BY t_street_name,t_reference,t_date_time_issued
order by t_date_time_issued
当前输出
2015-06-03 09:34:25.000 Airport (60/100 14 Day) 1
2015-06-03 09:38:06.000 Airport (60/100 14 Day) 1
2015-06-03 10:09:06.000 Airport (60/100 14 Day) 1
2015-06-03 10:14:16.000 Airport (60/100 14 Day) 1
2015-06-03 10:28:25.000 Airport (60/100 14 Day) 1
相反,我想得到:
2015-06-03 09:00:00.000 Airport (60/100 14 Day) 2
2015-06-03 10:00:00.000 Airport (60/100 14 Day) 3
答案 0 :(得分:1)
将GROUP BY
分成一个日期(使用CAST AS DATE
)和一小时(使用DATEPART
)。
SELECT CAST(t_date_time_issued AS DATE), DATEPART(hour, t_date_time_issued), t_street_name, count(t_reference)
FROM tickets
WHERE t_date_time_issued BETWEEN '03/06/2015' AND '03/07/2015'
AND t_street_name LIKE'%airport%'
GROUP BY t_street_name, t_reference, CAST(t_date_time_issued AS DATE), DATEPART(hour, t_date_time_issued)
ORDER BY t_date_time_issued
答案 1 :(得分:0)
您正在获得该结果,因为您也按日期分组,并且每行都有不同的日期。您应该只访问日期的小时而不需要分钟。