Getting the sum of several columns from two tables

前端 未结 5 477
生来不讨喜
生来不讨喜 2021-01-11 16:26

I want to get the sum of several columns from 2 different tables (these tables share the same structure).

If I only consider one table, I would write this kind of qu

5条回答
  •  离开以前
    2021-01-11 17:08

    I finally get this working using the Lieven's answer.

    Here is the correct code (amount1 = ... is not working on my environment, and there are too many ; in the query):

    SELECT MONTH_REF, SUM(sumAmount1), SUM(sumAmount2)
    FROM (
      SELECT MONTH_REF, SUM(amount1) as sumAmount1, SUM(amount2) as sumAmount1
          FROM T_FOO
          WHERE seller = XXX
          GROUP BY MONTH_REF
      UNION ALL SELECT MONTH_REF, SUM(amount1), SUM(amount2)
          FROM T_BAR
          WHERE seller = XXX
          GROUP BY MONTH_REF
      ) tmp
    GROUP BY MONTH_REF
    

提交回复
热议问题