SQL 3与group by和sum函数连接

时间:2017-02-25 09:03:18

标签: sql

我有3张桌子 Tbcodetable

    codevalue | codename | desc1 | desc2 
    1         | SATO     | NAG   | Naga
    2         | SATO     | BAG   | Baguio
    3         | SATO     | NCR   | Head Office

Tbmember

employeeno | capcon_accountno | savings_accountno | sato
1          | 00101            | 00201             | NCR
2          | 00102            | 00202             | BAG

Tbdeposit

employeeno | account_no | accountbalance
1          | 00101      | 1000
1          | 00201      | 5000
2          | 00102      | 1000
2          | 00202      | 5000

我想要查询的是一个查询中每个sato的资本和储蓄的总和

codename | codevalue | desc1      | desc2 | capcon | savings
SATO     |3          |Head Office |NCR    |1000    | 5000
SATO     |2          |Baguio      |BAG    |1000    | 5000
SATO     |1          |Naga        |NAG    |0       | 0

我能做的最好的查询是

SELECT codename,codevalue,desc1,desc2,sum(b.accountbalance) as capcon 
  FROM TBCODETABLE c 
  left join TBMEMBER a on c.desc2 = a.SatoCode 
  join tbdeposit b on a.employeeno = b.employeeno 
 where a.SLAStatus = 'A' and c.codename ='sato'
group by codename, codevalue, desc1, desc2

给了我这个结果

codename | codevalue | desc1      | desc2 | capcon
SATO     |1          |Head Office |NCR    |1000
SATO     |2          |Baguio      |BAG    |1000

这只给了我资本的总和(我输了如何包括储蓄和资本结果。)它不包括纳迦地区

我可以通过employeeno加入tbmember和tbdeposit,但后来我不知道如何在结果集中分离资本和储蓄的总和

修改
 我编辑了查询,现在我能够查询两个感谢syed,但我仍然无法获得NAG sato代码。 的查询:

SELECT codename,codevalue,desc1,desc2, coalesce(sum(b.accountbalance),0) as savings, coalesce(sum(bb.accountbalance),0) as capcon FROM TBCODETABLE c left join TBMEMBER a on c.desc2 = a.SatoCode join tbdeposit b on a.SAVINGS_AccountNo = b.AccountNo 
inner join tbdeposit bb on a.CAPCON_Accountno = bb.AccountNo where a.SLAStatus = 'A'
group by codename, codevalue, desc1, desc2

我能够得到预期的结果谢谢你们,克里斯的左边加入建议解决了我的NAG SATO问题,虽然Syed的回复帮助了我很大的时间。
我不赞成如何回复所以我只是赞成克里斯(第一次来这里。)

我的最终查询是:

SELECT codename,codevalue,desc1,desc2, coalesce(sum(b.accountbalance),0) as savings, coalesce(sum(bb.accountbalance),0) as capcon FROM TBCODETABLE c left join TBMEMBER a on c.desc2 = a.SatoCode left join tbdeposit b on a.SAVINGS_AccountNo = b.AccountNo 
left join tbdeposit bb on a.CAPCON_Accountno = bb.AccountNo where a.SLAStatus = 'A' or codename = 'sato'
group by codename, codevalue, desc1, desc2

3 个答案:

答案 0 :(得分:0)

首先,当你在LEFT JOIN编辑的桌子上加入某些内容时,你需要将其设为left join,否则你最终会得到一个与内连接相当的东西。这不是你唯一的问题。

其次,对于价值比较,案例很重要,因此如果您的表格样本正确,您应该c.codename ='SATO'

第三,您可能在此时想要在SUM内添加一个案例陈述,所以:

SUM(case 
    when account_no = capcon_account_no 
    THEN accountbalance ELSE NULL END)

你也可以为你的积蓄做类似的事情。

答案 1 :(得分:0)

我认为您需要在子查询中按employeenoaccount_no进行分组。然后使用case when

将capcon和储蓄拆分为单独的列
select codename,
       codevalue,
       desc1,
       desc2,
       sum( case when a.savings_accountno = b.account_no then accountbalance else 0 end ) as savings,
       sum( case when a.capcon_accountno = b.account_no then accountbalance else 0 end ) as capcon
from   tbcodetable as c
       left join
       tbmember as a
       on c.desc1 = a.sato
       left join
       (
         select employeeno,
                account_no,
                sum( accountbalance ) as accountbalance
         from   tbdeposit
         group by employeeno,
                account_no
       ) as b
       on a.employeeno = b.employeeno
 group by codename,
       codevalue,
       desc1,
       desc2

请参阅SQLfiddle here

答案 2 :(得分:0)

试试这个

select codename,codevalue,desc1,desc2,coalesce(sum(accountbalance),0) as capcon,
coalesce(deposite,0) as saving 
from tbcodetable tc  
left join tbmember tb 
on tc.codevalue = tb.employeeno  
left join(select max(employeeno) as employee,max(accountbalance) as deposite,accountbalance,employeeno from tbdeposite tp group by employeeno)l 
on tb.employeeno = l.employeeno group by codevalue;

DEMO