0
我有一个表,它看起来像你可以看到下面:如何统计一段时间(日期时间和日期时间)字段中每半小时的记录数?
我需要每半小时的记录数存在计算,输出应该是这样的:
Time Actual Count:
7:00 4
7:30 4
8:00 4
8:30 4
9:00 4
9:30 5
10:00 5
10:30 6
11:00 7
11:30 8
12:00 8
12:30 8
13:00 8
13:30 8
14:00 8
14:30 8
15:00 7
15:30 7
16:00 7
16:30 6
17:00 5
17:30 4
18:00 4
我已经尝试过做一个应该保持每半小时一次的帮助桌。我已经加入了这个包含数据的表的helpertable,之后我尝试使用一个按功能分组,但它不起作用。
我的查询是这样的:
Create table period (timefrom datetime, timeto datetime)
insert into period
select '1899-12-30 07:00:00.000', '1899-12-30 07:30:00.000'
Union all
select '1899-12-30 07:30:00.000', '1899-12-30 08:00:00.000'
select *
from period p left join table1 t on t.ActualTimeFrom < p.timeto and t.ActualTimeTo >=p.timefrom
Grouping this give me no desired result....
任何一个想法如何得出的结果?
P.s.我使用SQL Server 2005的
我应该用这条线的组中的部分?请将您的答案具体化为... – Amedo
在您的SELECT和GROUP BY中使用它,在WHERE子句中指定您的日期范围。例如WHERE your_date_column> your_start_date AND your_date_column