最佳答案
create table #Events
(
EventID int identity primary key,
StartDate datetime not null,
EndDate datetime not null
)
go
insert into #Events (StartDate, EndDate)
select '2007-01-01 12:44:12 AM', '2007-01-01 12:45:34 AM' union all
select '2007-01-01 12:45:12 AM', '2007-01-01 12:46:34 AM' union all
select '2007-01-01 12:46:12 AM', '2007-01-01 12:47:34 AM' union all
select '2007-01-02 5:01:08 AM', '2007-01-02 5:05:37 AM' union all
select '2007-01-02 5:50:08 AM', '2007-01-02 5:55:59 AM' union all
select '2007-01-03 4:34:12 AM', '2007-01-03 4:55:18 AM' union all
select '2007-01-07 3:12:23 AM', '2007-01-07 3:52:25 AM'
(为收获他们的基础 sql 向 http://www.sqlteam.com/article/working-with-time-spans-and-durations-in-sql-server道歉)
我试图找到一个小时内发生的事件的计数,因此结果集应该是这样的:
2007-01-01 12:00 3
2007-01-02 5:00 2
2007-01-03 4:00 1
2007-01-07 3:00 1
我一直在玩日期添加,轮流和分组,但没有得到它。有人能帮忙吗?
谢谢。