日期:2014-05-17 浏览次数:20651 次
create table test(userid int, datetime datetime)
insert into test
select 1 ,'2014-01-13' union all
select 2 ,'2014-01-13' union all
select 3 ,'2014-01-13' union all
select 1 ,'2014-01-14' union all
select 4 ,'2014-01-14'
go
IF object_id('tempdb..#t','U')IS NOT NULL
DROP TABLE #t
select *,
(select COUNT(*) from test t2
where t1.userid = t2.userid and t1.datetime>=t2.datetime) rownum INTO #t
from test t1
select datetime,COUNT(*) '人数',(SELECT COUNT(1) FROM #t b WHERE a.datetime=b.datetime)[总人数]
from #t a
where rownum = 1
group by datetime
/*
datetime 人数 总人数
----------------------- ----------- -----------
2014-01-13 00:00:00.000 3 3
2014-01-14 00:00:00.000 1 2
*/
--drop table test
create table test(userid int, datetime datetime)
insert into test
select 1 ,'2014