SQL Query to sum fields from different tables

前端 未结 5 1321
無奈伤痛
無奈伤痛 2020-12-30 15:57

I\'m a humble programmer that hates SQL ... :) Please help me with this query.

I have 4 tables, for example:

Table A:
Id Total
1  100
2  200
3  500

         


        
5条回答
  •  有刺的猬
    2020-12-30 16:22

    Or you can take advantage of using SubQueries:

    select A.ID, A.Total, b.SB as AmountB, c.SC as AmountC, d.SD as AmountD
    from A
      inner join (select ExtID, sum(Amount) as SB from B group by ExtID) b on A.ID = b.ExtID
      inner join (select ExtID, sum(Amount) as SC from C group by ExtID) c on c.ExtID = A.ID
      inner join (select ExtID, sum(Amount) as SD from D group by ExtID) d on d.ExtID = A.ID
    

提交回复
热议问题