select *
from
(
WITH t AS
(
SELECT 560 seq, 1 id, 'insrd_cd' item, '' v1, '2222222' v2, '20130903' sdt, '20130820' rdt FROM dual
UNION ALL SELECT 560, 1, 'insrd_nm' , '' , '노태자' , '20130903', '20130820' FROM dual
UNION ALL SELECT 560, 1, 'insrd_rlatn', '' , '02' , '20130903', '20130820' FROM dual
UNION ALL SELECT 560, 1, 'join_yn' , '' , '1' , '20130903', '20130820' FROM dual
UNION ALL SELECT 561, 1, 'insrd_cd' , '2222222', '2222223', '20130903', '20130820' FROM dual
UNION ALL SELECT 563, 1, 'insrd_nm' , '노태자' , '노태순' , '20130903', '20130820' FROM dual
UNION ALL SELECT 568, 1, 'insrd_cd' , '2222223', '2222224', '20130930', '20130821' FROM dual
UNION ALL SELECT 568, 1, 'insrd_nm' , '노태순' , '남태순' , '20130930', '20130821' FROM dual
UNION ALL SELECT 567, 1, 'insrd_cd' , '2222223', '' , '20131115', '20130821' FROM dual
UNION ALL SELECT 567, 1, 'insrd_nm' , '노태순' , '' , '20131115', '20130821' FROM dual
UNION ALL SELECT 567, 1, 'insrd_rlatn', '02' , '' , '20131115', '20130821' FROM dual
UNION ALL SELECT 567, 1, 'join_yn' , '1' , '' , '20131115', '20130821' FROM dual
)
SELECT MIN(insrd_cd ) KEEP(DENSE_RANK LAST ORDER BY seq) insrd_cd
, MIN(insrd_nm ) KEEP(DENSE_RANK LAST ORDER BY seq) insrd_nm
, MIN(insrd_rlatn) KEEP(DENSE_RANK LAST ORDER BY seq) insrd_rlatn
, sdt
, nvl(MAX(edt),'20140903') edt
, MAX(rdt) rdt
FROM (SELECT seq
, LAST_VALUE(MIN(DECODE(item, 'insrd_cd', v2))
IGNORE NULLS) OVER(ORDER BY sdt, seq) insrd_cd
, LAST_VALUE(MIN(DECODE(item, 'insrd_nm', v2))
IGNORE NULLS) OVER(ORDER BY sdt, seq) insrd_nm
, LAST_VALUE(MIN(DECODE(item, 'insrd_rlatn', v2))
IGNORE NULLS) OVER(ORDER BY sdt, seq) insrd_rlatn
, MAX(DECODE(item, 'join_yn', v1, '0')) join_yn
, sdt
, LEAD(sdt) OVER(ORDER BY sdt, seq) edt
, rdt
FROM t
WHERE id = 1
GROUP BY seq, sdt, rdt
)
WHERE join_yn = '0'
GROUP BY sdt
ORDER BY sdt
)
어제 마농님이 짜주진 위 쿼리에서..
(2222224 남태순 02 20130930 20131115 20130821) -> 이 최종 이력값 즉 마지막행만 ~ 나오게끔 구해야되는데...
row_number() over() 함수도 쓰고 rownum도 써봤는데..에러가나네요~ 도움부탁드립니다.ㅠㅠ