日期:2014-05-17 浏览次数:20442 次
--> 测试数据:@T
declare @T table([userid] varchar(3),[datetime] datetime)
insert @T
select 'aaa','2013-06-01' union all
select 'aaa','2013-06-01' union all
select 'bbb','2013-06-01' union all
select 'aaa','2013-06-02' union all
select 'ccc','2013-06-02' union all
select 'aaa','2013-06-03' union all
select 'ccc','2013-06-04' union all
select 'ddd','2013-06-04'
SELECT Convert(varchar(10),[datetime],120) AS [datetime] ,
( SELECT COUNT(DISTINCT userid)
FROM @t a
WHERE [datetime] = t.datetime
) - ( SELECT COUNT(DISTINCT userid)
FROM @t a
WHERE [datetime] < t.datetime
AND userid IN ( SELECT userid
FROM @T
WHERE [datetime] = t.[datetime] )
) AS 新增用户 ,