1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | WITH ta(mba_cd, reptdc_no, yy) AS ( SELECT 'KOF1' , 444, '2009' FROM dual UNION ALL SELECT 'KOO1' , 459, '2010' FROM dual UNION ALL SELECT 'KOF1' , 475, '2011' FROM dual ) SELECT yy , NVL2(reptdc_no, mba_cd, '' ) mba_cd , reptdc_no FROM ( SELECT b.yy , a.mba_cd , LAST_VALUE(a.reptdc_no IGNORE NULLS) OVER(PARTITION BY a.mba_cd ORDER BY b.yy) reptdc_no , ROW_NUMBER() OVER(PARTITION BY b.yy ORDER BY a.yy) rn FROM ( SELECT 2008 + LEVEL - 1 yy FROM dual CONNECT BY LEVEL <= TO_CHAR(sysdate, 'yyyy' ) - 2008 + 1 ) b LEFT OUTER JOIN ta a PARTITION BY (a.mba_cd) ON a.yy = b.yy ) WHERE reptdc_no IS NOT NULL OR rn = 1 ORDER BY yy, mba_cd ; |