Skip to content
Advertisement

SQL oracle: How can I get the results of 4 selects statements added & subtracted to get one value with a ‘WITH’ keyword

My code is:

WITH
A_EXP AS (SELECT SUM(VENDOR_ORDER_TOTAL) AS VOT
          FROM VENDOR_ORDER
          WHERE VENDOR_ORDER_STATUS = 'F'
          AND VENDOR_ORDER_DATE >= TO_DATE ('01 SEPTEMBER 2019', 'DD MONTH YYYY')
          AND VENDOR_ORDER_DATE <= TO_DATE ('30 SEPTEMBER 2019', 'DD MONTH YYYY')),

B_EXP AS (SELECT SUM(SALARY_PAID) AS SP
          FROM EMP_SALARY_INFO
          WHERE FROM_DATE >= TO_DATE ('01 SEPTEMBER 2019', 'DD MONTH YYYY')
          AND TILL_DATE <= TO_DATE ('30 SEPTEMBER 2019', 'DD MONTH YYYY')),

C_EXP AS (SELECT ROUND((COUNT(P.PAY_TYPE) * 0.10), 2) AS PT
          FROM CUSTOMER_PAYMENT P INNER JOIN CUSTOMER_ORDER O
          ON P.ORDER_ID = O.ORDER_ID
          WHERE P.PAY_TYPE = 'CARD'
          AND O.ORDER_DATE >= TO_DATE ('01 SEPTEMBER 2019', 'DD MONTH YYYY')
          AND O.ORDER_DATE <= TO_DATE ('30 SEPTEMBER 2019', 'DD MONTH YYYY')),

D_INC AS (SELECT SUM(ORDER_PRETAX) AS OP
          FROM CUSTOMER_ORDER
          WHERE PAY_STATUS = 'PAID'
          AND ORDER_DATE >= TO_DATE ('01 SEPTEMBER 2019', 'DD MONTH YYYY')
          AND ORDER_DATE <= TO_DATE ('30 SEPTEMBER 2019', 'DD MONTH YYYY'))

SELECT (D_INC - (A_EXP + B_EXP + C_EXP)) AS "$$ PROFIT/LOSS - SEPT 2019"
FROM A_EXP, B_EXP, C_EXP, D_INC

The result of each select statement in the with clause returns one value. I am trying to add the expenses and subtract it from the income. When I run the whole code I am getting some error which I am unable to figure out. Am I doing something wrong? I want the result from the aliases in this format [D_INC – (A_EXP + B_EXP + C_EXP)] and I want the result to be one value, i.e.,

EXPECTED OUTPUT:

 $$ PROFIT/LOSS - SEPT 2019
 --------------------------
          -1453.84

Advertisement

Answer

Please use below query , in last statement of your query I have just used column name instead of cte name .

WITH A_EXP
AS (
    SELECT SUM(VENDOR_ORDER_TOTAL) AS VOT
    FROM VENDOR_ORDER
    WHERE VENDOR_ORDER_STATUS = 'F'
        AND VENDOR_ORDER_DATE >= TO_DATE('01 SEPTEMBER 2019', 'DD MONTH YYYY')
        AND VENDOR_ORDER_DATE <= TO_DATE('30 SEPTEMBER 2019', 'DD MONTH YYYY')
    )
    ,B_EXP
AS (
    SELECT SUM(SALARY_PAID) AS SP
    FROM EMP_SALARY_INFO
    WHERE FROM_DATE >= TO_DATE('01 SEPTEMBER 2019', 'DD MONTH YYYY')
        AND TILL_DATE <= TO_DATE('30 SEPTEMBER 2019', 'DD MONTH YYYY')
    )
    ,C_EXP
AS (
    SELECT ROUND((COUNT(P.PAY_TYPE) * 0.10), 2) AS PT
    FROM CUSTOMER_PAYMENT P
    INNER JOIN CUSTOMER_ORDER O ON P.ORDER_ID = O.ORDER_ID
    WHERE P.PAY_TYPE = 'CARD'
        AND O.ORDER_DATE >= TO_DATE('01 SEPTEMBER 2019', 'DD MONTH YYYY')
        AND O.ORDER_DATE <= TO_DATE('30 SEPTEMBER 2019', 'DD MONTH YYYY')
    )
    ,D_INC
AS (
    SELECT SUM(ORDER_PRETAX) AS OP
    FROM CUSTOMER_ORDER
    WHERE PAY_STATUS = 'PAID'
        AND ORDER_DATE >= TO_DATE('01 SEPTEMBER 2019', 'DD MONTH YYYY')
        AND ORDER_DATE <= TO_DATE('30 SEPTEMBER 2019', 'DD MONTH YYYY')
    )
SELECT (OP - (VOT + SP + PT)) AS "$$ PROFIT/LOSS - SEPT 2019"
FROM A_EXP,B_EXP,C_EXP,D_INC
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement