SUM JOIN其他表返回双值SQL

时间:2019-01-11 14:24:03

标签: sql group-by aggregate-functions

我有2张桌子

enter image description here

请帮助我在下面返回结果

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以先执行JOIN,然后再进行求和:

SELECT p.code,  SUM(p.price) price_total, SUM(s.total) sale_total
FROM Produce p INNER JOIN
     (SELECT ID, SUM(salesoff) as total
      FROM Sale s
      GROUP BY ID
     ) s 
     ON s.ID = p.id
GROUP BY p.code;