表A:
uid     dat
 01     060801
 02     060801
 03     060801
 04     060802
 05     060802
 06     060803
 07     060804
表B:
 uid   pluid  amt
 01     11     1
 01     12     2
 02     12     1
 03      13     2
 04      12    1
 05      13    2
 06      11    1
 07      12    2
求:
dat      sum(amt)
060801     6
060802     3
060803     1
060807      2就是想根据A中的时间DAT把当天的销售总数做出来!
 

解决方案 »

  1.   

    select a.dat,sum(b.amt) as [sum(amt)]
    from a,b
    where a.uid=b.uid
    group by a.dat
    order by a.dat
      

  2.   

    select dat,sum(amt) from b left join a on a.uid=b.uid group by dat
      

  3.   

    create table A(uid char(2),dat char(6))
    insert A
    select '01','060801' union all
    select '02','060801' union all
    select '03','060801' union all
    select '04','060802' union all
    select '05','060802' union all
    select '06','060803' union all
    select '07','060804'
    --select * from Acreate table B(uid char(2),pluid int,amt int)
    insert B
    select  '01',      11,     1 union all
    select  '01',      12,     2 union all
    select  '02',      12,     1 union all
    select  '03',      13,     2 union all
    select  '04',      12,     1 union all
    select  '05',      13,     2 union all
    select  '06',      11,     1 union all
    select  '07',      12,     2
    --select * from Bselect
    A.dat,
    sum(B.amt) as [sum(amt)]
    from B inner join A on 
    B.uid=A.uid 
    group by A.datdrop table A,B
      

  4.   

    select A.dat,sum(B.amt) amt
    from A 
    inner join B
    on A.uid=B.uid
    group by A.dat