要用戶就加上,後面group by也加上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 allselect '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',3select 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 個資料列受到影響) */
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 allselect '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',3select 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 個資料列受到影響)
*/