우선 원하는건
년도 || 분기별 손익을 구하려고 하는데요
WITH M_DATA
AS
(
SELECT '2017_1' YYYYMM
,'C' AS GBN
,2486532576 AS AMT1
,149477800 AS AMT2
,0 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2018_1' YYYYMM
,'C' AS GBN
,5330058700 AS AMT1
,1014711124 AS AMT2
,97757700 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2019_1' YYYYMM
,'C' AS GBN
,9144568951 AS AMT1
,1644554334 AS AMT2
,614302836 AS AMT3
,58738100 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2020_1' YYYYMM
,'C' AS GBN
,25498676327 AS AMT1
,2529122730 AS AMT2
,668306945 AS AMT3
,347803129 AS AMT4
,36385000 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2017_2' YYYYMM
,'C' AS GBN
,2110904485 AS AMT1
,408033266 AS AMT2
,0 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2018_2' YYYYMM
,'C' AS GBN
,4101934000 AS AMT1
,1011141428 AS AMT2
,265803641 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2017_1' YYYYMM
,'G' AS GBN
,2110904485 AS AMT1
,408033266 AS AMT2
,0 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
UNION ALL
SELECT '2018_1' YYYYMM
,'G' AS GBN
,4101934000 AS AMT1
,1011141428 AS AMT2
,265803641 AS AMT3
,0 AS AMT4
,0 AS AMT5
,0 AS AMT6
FROM DUAL
)
SELECT *
FROM M_DATA
이 데이타를 가지고 첨부하는 화면 처럼 구성을 해서 계산을 하려고 합니다.
피벗을 사용해야 될거 같은데..어느방식으로 해야 원하는 데이타를 나오게 핡수 있을까요?
왜 이래야만 하는지 이해는 안가지만
억지스럽게 한번 만들어 봤습니다.
WITH m_data AS ( SELECT '2017_1' yyyymm, 'C' gbn, 2486532576 amt1, 149477800 amt2, 0 amt3, 0 amt4, 0 amt5, 0 amt6 FROM dual UNION ALL SELECT '2018_1', 'C', 5330058700, 1014711124, 97757700, 0, 0, 0 FROM dual UNION ALL SELECT '2019_1', 'C', 9144568951, 1644554334, 614302836, 58738100, 0, 0 FROM dual UNION ALL SELECT '2020_1', 'C', 25498676327, 2529122730, 668306945, 347803129, 36385000, 0 FROM dual UNION ALL SELECT '2017_2', 'C', 2110904485, 408033266, 0, 0, 0, 0 FROM dual UNION ALL SELECT '2018_2', 'C', 4101934000, 1011141428, 265803641, 0, 0, 0 FROM dual UNION ALL SELECT '2017_1', 'G', 2110904485, 408033266, 0, 0, 0, 0 FROM dual UNION ALL SELECT '2018_1', 'G', 4101934000, 1011141428, 265803641, 0, 0, 0 FROM dual ) SELECT gbn , NVL(MIN(DECODE(yyyymm, '2017_1', amt1)), 0) amt1 , NVL(MIN(DECODE(yyyymm, '2018_1', amt2)), 0) amt2 , NVL(MIN(DECODE(yyyymm, '2019_1', amt3)), 0) amt3 , NVL(MIN(DECODE(yyyymm, '2020_1', amt4)), 0) amt4 , NVL(MIN(DECODE(yyyymm, '2021_1', amt5)), 0) amt5 , NVL(MIN(DECODE(yyyymm, '2022_1', amt6)), 0) amt6 FROM m_data WHERE yyyymm IN ('2017_1', '2018_1', '2019_1', '2020_1', '2021_1', '2022_1') GROUP BY gbn ORDER BY gbn ;