如何使用不同的分组标准对多个列进行求和

时间:2013-08-27 07:46:48

标签: sql database oracle sum grouping

Currenly我正在做一个项目,我需要生成从表中提取的报告。 表格如下

CURRENCY_A AMOUNT_A CURRENCY_B AMOUNT_B CURRENCY_C AMOUNT_C

我的期望是货币(AMOUNT_A + AMOUNT_B + AMOUNT_C)组的总和 美元:
欧元:
POUNDS:

是否可以单独在oracle中执行此输出?

1 个答案:

答案 0 :(得分:0)

SELECT currency, SUM(total1) AS total FROM
(
SELECT currency_a AS currency, SUM(a_amount)  AS total1 FROM table1
GROUP BY currency_a
UNION ALL
SELECT currency_b, SUM(b_amount) AS total2 FROM table1
GROUP BY currency_b
UNION ALL
SELECT currency_c, SUM(c_amount) AS total2 FROM table1
GROUP BY currency_c
) T
GROUP BY currency