You are on page 1of 7

SELECT BUKRS, MNTH, WAERS, to_char(SUM(INV_TRANSACTION_AMT),'999,999,999,999.99') INV_TRANSACTION_A MT, to_char(SUM(INV_GLOBAL_AMT),'999,999,999,999.99') INV_GLOBAL_AMT FROM ( SELECT BSIS.BUKRS, SUBSTR(BKPF.

BUDAT, 5,2) MNTH, BSIS.WAERS, BSIS.BELNR, BSIS.BUZEI, SUM(CASE WHEN BSIS.SHKZG = 'S' THEN BSIS.WRBTR ELSE BSIS.WRBTR * -1 END) INV_TRANSACTION_AMT, SUM(CASE WHEN BSIS.SHKZG = 'S' THEN BSIS.DMBTR ELSE BSIS.DMBTR * -1 END) INV_GLOBAL_AMT FROM BSIS, (SELECT DISTINCT GJAHR, BUKRS, BELNR, BUZEI FROM ( SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS INNER JOIN BSAK ON BSAS.BELNR AND BSAS.BUKRS AND BSAS.GJAHR AND BSAK.BSCHL LFA1 WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012' AND BSAK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS INNER JOIN BSIK ON BSAS.BELNR AND BSAS.BUKRS AND BSAS.GJAHR AND BSIK.BSCHL LFA1 WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012' AND BSIK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI

= BSAK.BELNR = BSAK.BUKRS = BSAK.GJAHR IN ('21','22','31','32'),

= BSIK.BELNR = BSIK.BUKRS = BSIK.GJAHR IN ('21','22','31','32'),

FROM BSIS INNER JOIN BSAK ON BSIS.BELNR AND BSIS.BUKRS AND BSIS.GJAHR AND BSAK.BSCHL LFA1 WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' AND BSAK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI FROM BSIS INNER JOIN BSIK ON BSIS.BELNR AND BSIS.BUKRS AND BSIS.GJAHR AND BSIK.BSCHL LFA1 WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' AND BSIK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006'

= BSAK.BELNR = BSAK.BUKRS = BSAK.GJAHR IN ('21','22','31','32'),

= BSIK.BELNR = BSIK.BUKRS = BSIK.GJAHR IN ('21','22','31','32'),

MINUS SELECT BSAK.BELNR, BSAK.BUKRS, BSAK.GJAHR, BSAK.BUZEI FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL IN ('21','22','31','32') MINUS SELECT BSIK.BELNR, BSIK.BUKRS, BSIK.GJAHR, BSIK.BUZEI FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL IN ('21','22','31','32') MINUS SELECT BSAK.BELNR, BSAK.BUKRS, BSAK.GJAHR, BSAK.BUZEI FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BELNR IN (SELECT BSAK.BELNR FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL NOT IN ('21','22','31','32')) MINUS SELECT BSIK.BELNR, BSIK.BUKRS, BSIK.GJAHR, BSIK.BUZEI

FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BELNR IN (SELECT BSIK.BELNR FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL NOT IN ('21','22','31','32')) MINUS SELECT X.BELNR, X.BUKRS, X.GJAHR, X.BUZEI FROM (SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI FROM BSIS WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' UNION SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012') X WHERE X.BELNR IN (SELECT BSIK.BELNR FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL IN ('21','22','31','32') GROUP BY BSIK.BELNR HAVING COUNT(*) > 1) OR X.BELNR IN (SELECT BSAK.BELNR FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL IN ('21','22','31','32') GROUP BY BSAK.BELNR HAVING COUNT(*) > 1) ) ) A, BKPF WHERE BSIS.BUKRS = A.BUKRS AND BSIS.GJAHR = A.GJAHR AND BSIS.BELNR = A.BELNR AND BSIS.BUZEI = A.BUZEI AND BSIS.BUKRS = BKPF.BUKRS AND BSIS.GJAHR = BKPF.GJAHR AND BSIS.BELNR = BKPF.BELNR AND BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' AND BKPF.BUKRS = 'HK04' AND BKPF.GJAHR = '2012' GROUP BY BSIS.BUKRS, SUBSTR(BKPF.BUDAT, 5,2),

BSIS.WAERS, BSIS.BELNR, BSIS.BUZEI UNION SELECT BSAS.BUKRS, SUBSTR(BKPF.BUDAT, 5,2) MNTH, BSAS.WAERS, BSAS.BELNR, BSAS.BUZEI, SUM(CASE WHEN BSAS.SHKZG = 'S' THEN BSAS.WRBTR ELSE BSAS.WRBTR * -1 END) INV_TRANSACTION_AMT, SUM(CASE WHEN BSAS.SHKZG = 'S' THEN BSAS.DMBTR ELSE BSAS.DMBTR * -1 END) INV_GLOBAL_AMT FROM BSAS, (SELECT DISTINCT GJAHR, BUKRS, BELNR, BUZEI FROM ( SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS INNER JOIN BSAK ON BSAS.BELNR AND BSAS.BUKRS AND BSAS.GJAHR AND BSAK.BSCHL LFA1 WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012' AND BSAK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS INNER JOIN BSIK ON BSAS.BELNR AND BSAS.BUKRS AND BSAS.GJAHR AND BSIK.BSCHL LFA1 WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012' AND BSIK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI FROM BSIS INNER JOIN BSAK ON BSIS.BELNR AND BSIS.BUKRS AND BSIS.GJAHR AND BSAK.BSCHL LFA1

= BSAK.BELNR = BSAK.BUKRS = BSAK.GJAHR IN ('21','22','31','32'),

= BSIK.BELNR = BSIK.BUKRS = BSIK.GJAHR IN ('21','22','31','32'),

= BSAK.BELNR = BSAK.BUKRS = BSAK.GJAHR IN ('21','22','31','32'),

WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' AND BSAK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006' UNION SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI FROM BSIS INNER JOIN BSIK ON BSIS.BELNR AND BSIS.BUKRS AND BSIS.GJAHR AND BSIK.BSCHL LFA1 WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' AND BSIK.LIFNR = LFA1.LIFNR AND LFA1.KTOKK != '0006'

= BSIK.BELNR = BSIK.BUKRS = BSIK.GJAHR IN ('21','22','31','32'),

MINUS SELECT BSAK.BELNR, BSAK.BUKRS, BSAK.GJAHR, BSAK.BUZEI FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL IN ('21','22','31','32') MINUS SELECT BSIK.BELNR, BSIK.BUKRS, BSIK.GJAHR, BSIK.BUZEI FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL IN ('21','22','31','32') MINUS SELECT BSAK.BELNR, BSAK.BUKRS, BSAK.GJAHR, BSAK.BUZEI FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BELNR IN (SELECT BSAK.BELNR FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL NOT IN ('21','22','31','32')) MINUS SELECT BSIK.BELNR, BSIK.BUKRS, BSIK.GJAHR, BSIK.BUZEI FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BELNR IN (SELECT BSIK.BELNR FROM BSIK

WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL NOT IN ('21','22','31','32')) MINUS SELECT X.BELNR, X.BUKRS, X.GJAHR, X.BUZEI FROM (SELECT BSIS.BELNR, BSIS.BUKRS, BSIS.GJAHR, BSIS.BUZEI FROM BSIS WHERE BSIS.BUKRS = 'HK04' AND BSIS.GJAHR = '2012' UNION SELECT BSAS.BELNR, BSAS.BUKRS, BSAS.GJAHR, BSAS.BUZEI FROM BSAS WHERE BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012') X WHERE X.BELNR IN (SELECT BSIK.BELNR FROM BSIK WHERE BSIK.BUKRS = 'HK04' AND BSIK.GJAHR = '2012' AND BSIK.BSCHL IN ('21','22','31','32') GROUP BY BSIK.BELNR HAVING COUNT(*) > 1) OR X.BELNR IN (SELECT BSAK.BELNR FROM BSAK WHERE BSAK.BUKRS = 'HK04' AND BSAK.GJAHR = '2012' AND BSAK.BSCHL IN ('21','22','31','32') GROUP BY BSAK.BELNR HAVING COUNT(*) > 1) ) ) A, BKPF WHERE BSAS.BUKRS = A.BUKRS AND BSAS.GJAHR = A.GJAHR AND BSAS.BELNR = A.BELNR AND BSAS.BUZEI = A.BUZEI AND BSAS.BUKRS = BKPF.BUKRS AND BSAS.GJAHR = BKPF.GJAHR AND BSAS.BELNR = BKPF.BELNR AND BSAS.BUKRS = 'HK04' AND BSAS.GJAHR = '2012' AND BKPF.BUKRS = 'HK04' AND BKPF.GJAHR = '2012' GROUP BY BSAS.BUKRS, SUBSTR(BKPF.BUDAT, 5,2), BSAS.WAERS, BSAS.BELNR, BSAS.BUZEI ) GROUP BY BUKRS, MNTH,

WAERS ORDER BY BUKRS, MNTH, WAERS