select a.card_id,a.Card_name,m1.xc,m2.zc,m3.he,m4.sb,b.usecent,
c1.storecent-c2.usecent as totalcent,d.totalmoney
from
(select card_id,card_name from card ) as a, 
(select card_id,sum(storecent) as xc from store where storetype='xc' group by card_id ) as m1,
(select card_id,sum(StoreCent) as zc from store where storetype='zc' group by card_id ) as m2),
(select card_id,sum(storecent) as he from store where storetype='he' group by card_id ) as m3,
(select card_id,sum(storecent) as sb from store where storetype='sb' group by card_id ) as m4,
(Select Card_id,0-sum(usecent) as usecent from usecent groupy by Card_id) as b,
(Select care_id,sum(i.storecent) as storecent from store grouy by card_id) as c1,
(Select care_id,sum(i.usecent) as usecent from store group by card_id) as c2,
(Select card_id,sum(money) as totalmoney from store group by cardid) as d
where  
   a.card_id*=m1.card_id and a.card_id*=m2.card_id and a.card_id*=m3.card_id
   and a.card_id*=m4.card_id and a.card_id*=b.card_id and a.card_id*=d.card_id
   and a.card_id*=c1.card_id and a.card_id*=c2.card_id;

解决方案 »

  1.   


    select a.card_id,a.name,isnull(convert(varchar(20),(select sum(b.storecent) from store b where a.card_id=b.card_id and b.storetype='XC')),'') as XC,
    isnull(convert(varchar(20),(select sum(h.storecent) from store h where a.card_id=h.card_id and h.storetype='ZC')),'') as ZC,
    isnull(convert(varchar(20),(select sum(f.storecent) from store f where a.card_id=f.card_id and f.storetype='HE')),'') as HE,
    isnull(convert(varchar(20),(select sum(g.storecent) from store g where a.card_id=g.card_id and g.storetype='SB')),'') as SB,
    isnull(convert(varchar(20),(select -sum(e.usecent) from usecent e where a.card_id=e.card_id)),'') as usecent,
    (isnull(convert(decimal(18,2),(select sum(j.storecent) from store j where j.card_id=a.card_id)),0)-
                     isnull(convert(decimal(18,2),(select sum(e.usecent) from usecent e where a.card_id=e.card_id)),0)) as TOTALcent,
    isnull(convert(varchar(20),(select sum(i.money) from store i where i.card_id=a.card_id)),'') as TOTALmoney
    from card a结果是你上面的TOTALcent有点错误!
      

  2.   

    select *
    ,(select sum(storecent) from store where storetype = 'XC' and card_id = card.card_id ) as XC
    ,(select sum(storecent) from store where storetype = 'ZC' and card_id = card.card_id ) as ZC
    ,(select sum(storecent) from store where storetype = 'HE' and card_id = card.card_id ) as HE
    ,(select sum(storecent) from store where storetype = 'SB' and card_id = card.card_id ) as SB
    ,-(select sum(usecent) from usecent where card_id = card.card_id) as  usecent
    ,(select sum(storecent) from store where card_id = card.card_id ) 
      -(select sum(usecent) from usecent where card_id = card.card_id) as TOTALcent
    ,(select sum([money]) from store where card_id = card.card_id ) as TOTALmoney
    from card
      

  3.   

    001      wang       10   30        -39      1          210
    002      zhang  60       10                 70        1025
    查询结果 30 和10 似乎位置颠倒了!
      

  4.   

    看到有贴子说: 子查询比join的效率还要高。