关于两个日期间
ID 开始时刻 结束时刻
134217743 2007-01-22 23:00:06.0002007-01-23 23:04:34.000
134217838 2007-01-23 22:52:20.0002007-01-23 23:04:44.000
现在需要统计出,每天每个小时每类ID的在线人数,只要有在某个小时段内就算在线一次,如第二条记录1342178382007-01-23 22:52:20.000 2007-01-23 23:04:44.000
则需统计出:
134217838 2007-01-23 22
134217838 2007-01-23 23
第一条则统计出:
134217743 2007-01-22 23
134217743 2007-01-23 0
134217743 2007-01-23 1
。
。
。
134217743 2007-01-23 22
134217743 2007-01-23 23
谢谢各位!帮忙解决问题,帮忙顶!
[解决办法]
declare @a table(ID varchar(20),开始时刻 smalldatetime,结束时刻 smalldatetime)
insert @a select '134217743 ', '2007-01-22 23:00:06.000 ', '2007-01-23 23:04:34.000 '
union all select '134217838 ', '2007-01-23 22:52:20.000 ', '2007-01-23 23:04:44.000 '
select top 100 id=identity( int,0,1) into # from syscolumns a,syscolumns b
select a.id,
convert(varchar(10),dateadd(hour,b.id,开始时刻),120)日期,
datepart(hour,dateadd(hour,b.id,开始时刻)) 时间数
from @a a,# b
where datediff(hour,dateadd(hour,b.id,开始时刻),结束时刻)> =0
order by a.id,日期,时间数
drop table #