数据库按天汇总,去重的有关问题
数据库按天汇总,去重的问题
要求按时间字段汇总,并且 按用户id去重,就是说 比如2 号的结果中的用户id是之前没有出现过的
------解决方案--------------------
用测试表和数据来表示吧!说的没看明白。
------解决方案--------------------
------解决方案--------------------
要用戶就加上,後面group by也加上
要求按时间字段汇总,并且 按用户id去重,就是说 比如2 号的结果中的用户id是之前没有出现过的
------解决方案--------------------
用测试表和数据来表示吧!说的没看明白。
------解决方案--------------------
------解决方案--------------------
要用戶就加上,後面group by也加上
- SQL code
declare @t table (U_Id varchar(20),D datetime,Q float) insert into @t select 'A','2012/3/8',2 union all select 'A','2012/3/8',1 union all select 'C','2012/3/8',1 union all select 'A','2012/3/9',1 union all select 'B','2012/3/9',5 union all select 'C','2012/3/9',1 union all select 'C','2012/3/9',3 select B.D as '时间',sum(A.Q) as '订单数' from @t as A, (select U_Id,min(D) as D from @t group by U_Id) as B where A.U_Id=B.U_Id and A.D=B.D group by B.D /* (7 個資料列受到影響) 时间 订单数 ----------------------- ---------------------- 2012-03-08 00:00:00.000 4 2012-03-09 00:00:00.000 5 (2 個資料列受到影響) */