具有标识列值的Oracle SQL累计和

时间:2014-08-12 21:45:28

标签: sql oracle

我正在使用Oracle SQL,我正在尝试使用Union All将两个表与Sums组合在一起,但我想将两个返回的行标识为“LIVE”和“ACTUALS”。我的查询结构如下......

select
SUM(JAN_2014) as JAN_14,
SUM(FEB_2014) AS FEB_14,
SUM(MAR_2014) AS MAR_14
from table A

union all

select
SUM(JAN_2014) as JAN_14,
SUM(FEB_2014) AS FEB_14,
SUM(MAR_2014) AS MAR_14
from table B

我想要的输出是......

DATA_VIEW  JAN_14  FEB_14  MAR_14
ACTUALS    10      15      20
LIVE       20      20      20

感谢您的帮助,

1 个答案:

答案 0 :(得分:2)

SELECT  'ACTUALS' AS DATA_VIEW,
        SUM(JAN_2014) as JAN_14,
        SUM(FEB_2014) AS FEB_14,
        SUM(MAR_2014) AS MAR_14
FROM tableA

UNION ALL

SELECT  'LIVE' AS DATA_VIEW,
        SUM(JAN_2014) AS JAN_14,
        SUM(FEB_2014) AS FEB_14,
        SUM(MAR_2014) AS MAR_14
FROM tableB