A1 | A2 | A3 | A4 | APPR | APPRDT | APPRST | ATT1 | ATT2 | ATT3 | ATT4 |
NBA | NBAB | 1 | A | james | 2018-03-02 | 승인 | 승인합니다 | |||
NBA | NBAB | 1 | B | davis | 2018-03-02 | 승인 | 승인합니다 | |||
NBA | NBAB | 2 | A | james | 2018-04-05 | 반려 | ||||
NBA | NBAB | 3 | A | |||||||
NBA | NBAB | 4 | A | ball | 2018-02-02 | 승인 | ||||
NBA | NBAB | 4 | B | ingram | 2018-02-02 | 반려 | 반려합니다 | |||
NBA | NBAB | 5 | A | |||||||
NBA | NBAB | 6 | A | |||||||
NBA | NBAB | 7 | A | walker | 2018-04-01 | 승인 | ||||
NBA | NBAB | 7 | B | middleton | 2018-04-01 | 반려 | ||||
NBA | NBAB | 8 | A | bird | 2018-04-11 | 승인 | ||||
NBA | NBAB | 8 | B | mchale | 2018-04-11 | 승인 | ||||
NBA | NBAB | 9 | A | |||||||
NBA | NBAB | 10 | A | parish | 2018-04-15 | 승인 | ||||
NBA | NBAB | 10 | B |
결과
No | 결재자1 | 결재일1 | 상태1 | 결재자2 | 결재일2 | 상태2 |
1 | james | 2018-03-02 | 승인 | davis | 2018-03-02 | 승인 |
2 | james | 2018-04-05 | 반려 | |||
3 | ||||||
4 | ball | 2018-02-02 | 승인 | ingram | 2018-02-02 | 반려 |
5 | ||||||
6 | ||||||
7 | walker | 2018-04-01 | 승인 | middleton | 2018-04-01 | 반려 |
8 | bird | 2018-04-11 | 승인 | mchale | 2018-04-11 | 승인 |
9 | ||||||
10 | parish | 2018-04-15 | 승인 |
TABLE명 : TB_NA
PRIMARY KEY : A1,A2,A3,A4
NO : A3
결재자1 : APPR
결재일1 : APPRDT
상태1 : APPRST
결재자2 : APPR
결재일2 : APPRDT
상태2 : APPRST
테이블은 1개의 테이블을 사용하고, A4를 기준으로 결재자2가 있으면 옆으로 조회하여
아래와 같은 결과를 얻으려고 합니다.
고수님들의 조언 부탁 드립니다. 감사합니다.
with t(A1, A2, A3, A4, APPR, APPRDT, APPRST, ATT1) as ( select 'NBA', 'NBAB', 1 , 'A', 'james', '2018-03-02', '승인', '승인합니다' from dual union all select 'NBA', 'NBAB', 1 , 'B', 'davis', '2018-03-02', '승인', '승인합니다' from dual union all select 'NBA', 'NBAB', 2 , 'A', 'james', '2018-04-05', '반려', '' from dual union all select 'NBA', 'NBAB', 3 , 'A', '', '', '', '' from dual union all select 'NBA', 'NBAB', 4 , 'A', 'ball', '2018-02-02', '승인', '' from dual union all select 'NBA', 'NBAB', 4 , 'B', 'ingram', '2018-02-02', '반려', '반려합니다' from dual union all select 'NBA', 'NBAB', 5 , 'A', '', '', '', '' from dual union all select 'NBA', 'NBAB', 6 , 'A', '', '', '', '' from dual union all select 'NBA', 'NBAB', 7 , 'A', 'walker', '2018-04-01', '승인', '' from dual union all select 'NBA', 'NBAB', 7 , 'B', 'middleton', '2018-04-01', '반려', '' from dual union all select 'NBA', 'NBAB', 8 , 'A', 'bird', '2018-04-11', '승인', '' from dual union all select 'NBA', 'NBAB', 8 , 'B', 'mchale', '2018-04-11', '승인', '' from dual union all select 'NBA', 'NBAB', 9 , 'A', '', '', '', '' from dual union all select 'NBA', 'NBAB', 10, 'A', 'parish', '2018-04-15', '승인', '' from dual union all select 'NBA', 'NBAB', 10, 'B', '', '', '', '' from dual ) select a1, a2, a3 , max(decode(a4,'A',appr)) appr1, max(decode(a4,'A',apprdt)) apprdt1, max(decode(a4,'A',apprst)) apprst1 , max(decode(a4,'B',appr)) appr2, max(decode(a4,'B',apprdt)) apprdt2, max(decode(a4,'B',apprst)) apprst2 from t group by a1, a2, a3 order by a3
-- 결재일과 상태가 같은때 순번을 정해야한다면 조건이 있어야합니다. WITH T ( A1,A2,A3,A4,APPR,APPRDT,APPRST,ATT1 ) AS ( SELECT 'NBA','NBAB',1 ,'A', 'james' ,'2018-03-02', '승인', '승인합니다' FROM DUAL UNION ALL SELECT 'NBA','NBAB',1 ,'B', 'davis' ,'2018-03-02', '승인', '승인합니다' FROM DUAL UNION ALL SELECT 'NBA','NBAB',2 ,'A', 'james' ,'2018-04-05', '반려', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',3 ,'A', '' ,'' , ' ', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',4 ,'A', 'ball' ,'2018-02-02', '승인', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',4 ,'B', 'ingram' ,'2018-02-02', '반려', '반려합니다' FROM DUAL UNION ALL SELECT 'NBA','NBAB',5 ,'A', '' ,'' , '' , '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',6 ,'A', '' ,'' , '' , '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',7 ,'A', 'walker' ,'2018-04-01', '승인', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',7 ,'B', 'middleton' ,'2018-04-01', '반려', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',8 ,'A', 'bird' ,'2018-04-11', '승인', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',8 ,'B', 'mchale' ,'2018-04-11', '승인', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',9 ,'A', '' ,'' , '' , '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',10 ,'A', 'parish' ,'2018-04-15', '승인', '' FROM DUAL UNION ALL SELECT 'NBA','NBAB',10 ,'B', '' ,'' , '' , '' FROM DUAL ) SELECT A1 , A2 , A3 , MIN(CASE WHEN RN=1 THEN APPR END) AS "결재자1" , MIN(CASE WHEN RN=1 THEN APPRDT END) AS "결재일1" , MIN(CASE WHEN RN=1 THEN APPRST END) AS "상태1" , MIN(CASE WHEN RN=2 THEN APPR END) AS "결재자2" , MIN(CASE WHEN RN=2 THEN APPRDT END) AS "결재일2" , MIN(CASE WHEN RN=2 THEN APPRST END) AS "상태2" FROM (SELECT A1 , A2 , A3 , APPR , APPRDT , APPRST , ROW_NUMBER() OVER(PARTITION BY A1 ,A2 ,A3 ORDER BY APPRDT , APPRST DESC NULLS LAST ) RN FROM T ) A GROUP BY A1,A2,A3
SELECT a.NO, "APPR1","APPRDT1","APPRST1","APPR2","APPRDT2","APPRST2"
FROM
(
SELECT A3 as No, A4, APPR as "APPR1", APPRDT as "APPRDT1", APPRST as "APPRST1"
FROM queryTEST
WHERE A4 = 'A'
order by A3
) a
LEFT OUTER JOIN
(
SELECT A3 as No, A4, APPR as "APPR2", APPRDT as "APPRDT2", APPRST as "APPRST2"
FROM queryTEST
WHERE A4 = 'B'
order by A3
) b
ON a.No = b.No;
인덱스는 A4,A3 순서로 생성하면 될 것 같네요