Skip to content
Advertisement

How to group two table with the same columns doing total of a column value

I have two oracle table with the same columns I want to create a query over those two tables doing total by a column : Table 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  

Table 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  

I want to create a select query that allow to have one table with total calculated based on column card_type and date_creation

Query results should be : Table 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  

Advertisement

Answer

You need to put together with UNION ALL and then 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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement