안녕하세요! 질문좀 드릴려고 글 올렸습니다!
결과셋은 아래와 같으며 제가 원하는것은
MOD(ROW_NUMBER() OVER(PARTITION BY PAY_DEDU_FLAG ORDER BY PAY_ITEM_CD),2) 이 값이 0일땐 좌측, 1일땐 우측에 출력되어
총 72로우로 출력되는 데이터를 널값 없이 36로우로 출력되는 데이터로 뽑고 싶습니다!..
피벗 개념으로 접근해야되는건지 아니면 처음부터 접근이 잘못된건지 잘모르겠습니다.. 현재는 아래와 같이
진행 하였습니다.. 해결책이 있을까요?
SELECT DECODE(IT.RN,0,IT.PRNT_NM) AS PRNT_NM_1 , SUM(DECODE(IT.RN,0,NVL(IP.PAY_ITEM_AMT,0))) AS PAY_ITEM_AMT_1 , DECODE(IT.RN,0,CASE WHEN IT.PAY_DEDU_FLAG IN ('1','3') THEN '1' ELSE '2' END) AS PAY_DEDU_FLAG_1 , DECODE(IT.RN,1,IT.PRNT_NM) AS PRNT_NM_2 , SUM(DECODE(IT.RN,1,NVL(IP.PAY_ITEM_AMT,0))) AS PAY_ITEM_AMT_2 , DECODE(IT.RN,1,CASE WHEN IT.PAY_DEDU_FLAG IN ('1','3') THEN '1' ELSE '2' END) AS PAY_DEDU_FLAG_2 , IT.RN FROM MPP_MMPAYMST MM INNER JOIN MPB_PAYPRFITM PR ON MM.PAY_CLS_PRFSHIP_FLAG = PR.PAY_CLS_PRFSHIP_FLAG LEFT OUTER JOIN MPP_PAYITEMPYDTL IP ON MM.EMP_NO = IP.EMP_NO AND MM.PY_YM = IP.PY_YM AND MM.PAY_PY_FLAG = IP.PAY_PY_FLAG AND PR.PAY_ITEM_CD = IP.PAY_ITEM_CD LEFT OUTER JOIN (SELECT PAY_ITEM_CD,PAY_DEDU_FLAG,PRNT_NM,MOD(ROW_NUMBER() OVER(PARTITION BY PAY_DEDU_FLAG ORDER BY PAY_ITEM_CD),2) RN FROM MPB_PAYITEM) IT ON PR.PAY_ITEM_CD = IT.PAY_ITEM_CD WHERE MM.EMP_NO = '962001' AND MM.PY_YM = '201606' AND MM.PAY_PY_FLAG = '1' GROUP BY PAY_DEDU_FLAG,PR.PRNT_SEQ,IT.RN,IT.PRNT_NM ORDER BY PR.PRNT_SEQ ; 원하는 데이터 셋
PRNT_NM_1 PAY_ITEM_AMT_1 PAY_DEDU_FLAG_1 PRNT_NM_2 PAY_ITEM_AMT_2 PAY_DEDU_FLAG_2
직무급 948000 1 근속급 1381000 1
기술업무 15000 1 보직수당 0 1
.
.
.
.
.
.
사학연금대부 585560 2 사학연금 410990 2
고용보험 0 2 건강보험 196000 2
현재 결과
아래 그림이 위의 쿼리 결과로 나온 데이터 셋인데요.. 사실 현재 상태는 그룹바이 없이도 아래 그림과 같이 출력이 가능한데 그룹바이와 디코드를 사용해서 어떻게 해결 해보려고 했는데 안되서 어떻게 해야될지 갈피가 안잡혀서 글 쓰게 되었습니다..
SELECT DECODE(IT.RN,0,IT.PRNT_NM) AS PRNT_NM_1 , DECODE(IT.RN,0,NVL(IP.PAY_ITEM_AMT,0)) AS PAY_ITEM_AMT_1 , DECODE(IT.RN,0,CASE WHEN IT.PAY_DEDU_FLAG IN ('1','3') THEN '1' ELSE '2' END) AS PAY_DEDU_FLAG_1 , DECODE(IT.RN,1,IT.PRNT_NM) AS PRNT_NM_2 , DECODE(IT.RN,1,NVL(IP.PAY_ITEM_AMT,0)) AS PAY_ITEM_AMT_2 , DECODE(IT.RN,1,CASE WHEN IT.PAY_DEDU_FLAG IN ('1','3') THEN '1' ELSE '2' END) AS PAY_DEDU_FLAG_2 , IT.RN FROM MPP_MMPAYMST MM INNER JOIN MPB_PAYPRFITM PR ON MM.PAY_CLS_PRFSHIP_FLAG = PR.PAY_CLS_PRFSHIP_FLAG LEFT OUTER JOIN MPP_PAYITEMPYDTL IP ON MM.EMP_NO = IP.EMP_NO AND MM.PY_YM = IP.PY_YM AND MM.PAY_PY_FLAG = IP.PAY_PY_FLAG AND PR.PAY_ITEM_CD = IP.PAY_ITEM_CD LEFT OUTER JOIN (SELECT PAY_ITEM_CD,PAY_DEDU_FLAG,PRNT_NM,MOD(ROW_NUMBER() OVER(PARTITION BY PAY_DEDU_FLAG ORDER BY PAY_ITEM_CD),2) RN FROM MPB_PAYITEM) IT ON PR.PAY_ITEM_CD = IT.PAY_ITEM_CD WHERE MM.EMP_NO = '962001' AND MM.PY_YM = '201606' AND MM.PAY_PY_FLAG = '1' ORDER BY PR.PRNT_SEQ ;
이결과로 나온 데이터 셋이 위 그림과 같습니다
SELECT pay_dedu_flag , MIN(DECODE(MOD(rn, 2), 1, prnt_nm )) prnt_nm_1 , MIN(DECODE(MOD(rn, 2), 1, pay_item_amt)) pay_item_amt_1 , MIN(DECODE(MOD(rn, 2), 0, prnt_nm )) prnt_nm_2 , MIN(DECODE(MOD(rn, 2), 0, pay_item_amt)) pay_item_amt_2 FROM (SELECT it.prnt_nm , NVL(ip.pay_item_amt, 0) pay_item_amt , CASE WHEN it.pay_dedu_flag IN ('1','3') THEN '1' ELSE '2' END AS pay_dedu_flag , ROW_NUMBER() OVER( PARTITION BY CASE WHEN it.pay_dedu_flag IN ('1','3') THEN '1' ELSE '2' END ORDER BY pr.prnt_seq ) rn FROM mpp_mmpaymst mm INNER JOIN mpb_payprfitm pr ON mm.pay_cls_prfship_flag = pr.pay_cls_prfship_flag LEFT OUTER JOIN mpp_payitempydtl ip ON mm.emp_no = ip.emp_no AND mm.py_ym = ip.py_ym AND mm.pay_py_flag = ip.pay_py_flag AND pr.pay_item_cd = ip.pay_item_cd INNER JOIN mpb_payitem it ON pr.pay_item_cd = it.pay_item_cd WHERE mm.emp_no = '962001' AND mm.py_ym = '201606' AND mm.pay_py_flag = '1' ) GROUP BY pay_dedu_flag, CEIL(rn / 2) ORDER BY pay_dedu_flag, CEIL(rn / 2) ;