with tbl1 as ( select '0001' code, '1' seq, '131' changeCode, '메롱' status from dual union all select '0001' code, '2' seq, '201' changeCode, '정상' status from dual union all select '0001' code, '3' seq, '202' changeCode, '정상' status from dual union all select '0001' code, '4' seq, '603' changeCode, '메롱' status from dual union all select '0002' code, '1' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '3' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '4' seq, '100' changeCode, '정상' status from dual union all select '0003' code, '1' seq, '100' changeCode, '메롱' status from dual union all select '0003' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0003' code, '3' seq, '100' changeCode, '정상' status from dual ), tbl2 as ( select '0001' code, '1' status from dual union all select '0002' code, '1' status from dual union all select '0003' code, '3' status from dual )select code, changeCode, status from (select a.code, changecode, a.status, row_number() over (partition by a.code order by seq desc) as rn from tbl1 a, tbl2 b where a.code = b.code and b.status = '1' )where rn = 1 ;
with tbl1 as ( select '0001' code, '1' seq, '131' changeCode, '메롱' status from dual union all select '0001' code, '2' seq, '201' changeCode, '정상' status from dual union all select '0001' code, '3' seq, '202' changeCode, '헤롱' status from dual union all select '0001' code, '4' seq, '603' changeCode, '메롱' status from dual union all select '0002' code, '1' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '3' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '4' seq, '100' changeCode, '정상' status from dual union all select '0003' code, '1' seq, '100' changeCode, '메롱' status from dual union all select '0003' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0003' code, '3' seq, '100' changeCode, '정상' status from dual ), tbl2 as ( select '0001' code, '1' status from dual union all select '0002' code, '1' status from dual union all select '0003' code, '3' status from dual )select gb,code, changecode, status from ( select 'non' gb , a.code , a.changecode , a.status , dense_rank() over(partition by a.code order by seq desc) rn from tbl1 a , tbl2 b where a.code = b.code and b.status = '1' and a.changecode <> '603' ) where rn = 1 union all select gb, code, changecode, status from ( select 'is' gb , a.code , a.changecode , a.status , dense_rank() over(partition by a.code order by seq desc) rn from tbl1 a , tbl2 b where a.code = b.code and b.status = '1' and a.changecode = '603' ) where rn = 3 ;
-- Oracle 11g의 LISTAGG 로 해봤어요.. SELECT CODE , regexp_substr(c_list, '[^,]+', 1, DECODE(INSTR(c_list, '603'), 1, 3, 1)) changeCode , regexp_substr(s_list, '[^,]+', 1, DECODE(INSTR(c_list, '603'), 1, 3, 1)) status FROM (SELECT A.CODE , LISTAGG(a.changeCode, ',') WITHIN GROUP(ORDER BY SEQ DESC) c_list , LISTAGG(a.status, ',') WITHIN GROUP(ORDER BY SEQ DESC) s_list FROM TBL1 A , TBL2 B WHERE A.CODE = B.CODE AND B.STATUS = '1' GROUP BY A.CODE )
--나만 별문제 아니라고 보는건가??;; with tbl1 as ( select '0001' code, '1' seq, '131' changeCode, '메롱' status from dual union all select '0001' code, '2' seq, '201' changeCode, '정상' status from dual union all select '0001' code, '3' seq, '202' changeCode, '정상' status from dual union all select '0001' code, '4' seq, '603' changeCode, '메롱' status from dual union all select '0002' code, '1' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '3' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '4' seq, '100' changeCode, '정상' status from dual union all select '0003' code, '1' seq, '100' changeCode, '메롱' status from dual union all select '0003' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0003' code, '3' seq, '100' changeCode, '정상' status from dual ), tbl2 as ( select '0001' code, '1' status from dual union all select '0002' code, '1' status from dual union all select '0003' code, '3' status from dual ) SELECT A.CODE, MIN(changeCode) AS changeCode, A.status FROM TBL1 A , TBL2 B WHERE A.CODE = B.CODE AND B.STATUS = '1' AND A.status = '정상' GROUP BY A.CODE, A.STATUS
with tbl1 as ( select '0001' code, '1' seq, '131' changeCode, '메롱' status from dual union all select '0001' code, '2' seq, '201' changeCode, '정상' status from dual union all select '0001' code, '3' seq, '202' changeCode, '정상' status from dual union all select '0001' code, '4' seq, '603' changeCode, '메롱' status from dual union all select '0002' code, '1' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '3' seq, '100' changeCode, '헤롱' status from dual union all select '0002' code, '4' seq, '100' changeCode, '정상' status from dual union all select '0003' code, '1' seq, '100' changeCode, '메롱' status from dual union all select '0003' code, '2' seq, '100' changeCode, '헤롱' status from dual union all select '0003' code, '3' seq, '100' changeCode, '정상' status from dual ), tbl2 as ( select '0001' code, '1' status from dual union all select '0002' code, '1' status from dual union all select '0003' code, '3' status from dual ) Select * From ( select rn, code, changeCode, status, Case When changeCode = '603' Then Lead(status,2) OVER(PARTITION BY code ORDER BY seq desc) Else status End status2 from (select a.code, changecode, a.status, seq, row_number() over (partition by a.code order by seq desc) as rn from tbl1 a, tbl2 b where a.code = b.code and b.status = '1' ) )where rn = 1 ;