要求按时间字段汇总,并且 按用户id去重,就是说 比如2 号的结果中的用户id是之前没有出现过的

解决方案 »

  1.   

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

  2.   

    订单表 OrderId,UserId,Time 只要在这一个表中查询即可,要求当天的记录中的UserId在上一天中没有出现过即可