1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | WITH t AS ( SELECT '20120201' dt, 'A' v FROM dual UNION ALL SELECT '20120101' , 'B' FROM dual UNION ALL SELECT '20111201' , 'B' FROM dual UNION ALL SELECT '20111101' , 'A' FROM dual UNION ALL SELECT '20111001' , 'B' FROM dual UNION ALL SELECT '20110901' , 'A' FROM dual UNION ALL SELECT '20110801' , 'B' FROM dual UNION ALL SELECT '20110701' , 'B' FROM dual UNION ALL SELECT '20110601' , 'A' FROM dual ) SELECT MAX (dt) dt FROM ( SELECT dt, v , LEAD(v) OVER( ORDER BY dt DESC ) x FROM t ) WHERE v = 'B' AND NVL(x, 'x' ) != 'B' ; |