我有两个具有相同列的oracle表,我想对这两个表进行按列总计的查询: 表1:
PRODUCTION_SYSTEM_ID CARD_TYPE TOTAL DATE_CREATION
-------------------- ---------- ---------- -------------
1 OPIC 1 28/09/18
1 IC 2 29/11/18
1 IC 1 27/09/18
表2:
PRODUCTION_SYSTEM_ID CARD_TYPE TOTAL DATE_CREATION
-------------------- ---------- ---------- -------------
1 OPIC 1 28/09/18
1 IC 2 29/11/18
1 IC 2 27/09/18
我想创建一个选择查询,该查询允许一个表的总值基于card_type和date_creation列
查询结果应为: 表3:
PRODUCTION_SYSTEM_ID CARD_TYPE TOTAL DATE_CREATION
-------------------- ---------- ---------- -------------
1 OPIC 2 28/09/18
1 IC 4 29/11/18
1 IC 3 27/09/18
答案 0 :(得分:3)
您需要先与UNION ALL
和GROUP BY
放在一起:
SELECT PRODUCTION_SYSTEM_ID
,CARD_TYPE
,SUM(TOTAL) TOTAL
,DATE_CREATION
FROM
(
SELECT PRODUCTION_SYSTEM_ID
,CARD_TYPE
,TOTAL
,DATE_CREATION
FROM TABLE1
UNION ALL
SELECT PRODUCTION_SYSTEM_ID
,CARD_TYPE
,TOTAL
,DATE_CREATION
FROM TABLE2
) S
GROUP BY
PRODUCTION_SYSTEM_ID
,CARD_TYPE
,DATE_CREATION