表a:
ID     DETAIL  NO    ACCOUNT
1       A       1      100
2       C       1       50
3       B       2       40
表b:
ID    NO      NUMBER
1      1         350
2      2         200
3      1         100我要得到:满足NO=1的account 和 number 的分类汇总
select sum(a.account),sum(b.number) from a join b on a.no=b.no where a.no=1
但结果不是 150,450

解决方案 »

  1.   

    select 
    (select sum(acccount) from a where no=1) as Aaccount,
    (select sum(number) from b where no=1) as Baccount
      

  2.   

    create table a
    (id int identity(1,1),
    detail char(1),
    no int,
    account int)
    insert a(detail,no,account)
    select 'A',1,100 UNION ALL
    select 'C',1,50  UNION ALL
    select 'B',2,100 
    --select * from acreate table b
    (id int identity(1,1),
    no int,
    number int)
    insert b(no,number)
    select 1,350 union all
    select 2,200 union all
    select 1,100
    --select * from bselect 
    (select sum(account) from a where no=1) as Aaccount,
    (select sum(number) from b where no=1) as Baccountdrop table a,b
      

  3.   

    连接的表的键不唯一,join 后sum 会大很多
      

  4.   

    select sum(account),sum([number]) 
    from (
    select  ACCOUNT,0 as [number]
    from a 
    where [no]=1
    union all
    select  0 as ACCOUNT,[number]
    from b
    where [no]=1
    ) as t