WITH T AS ( SELECT 1 AS INPT FROM DUAL UNION SELECT 2 AS INPT FROM DUAL UNION SELECT 3 AS INPT FROM DUAL UNION SELECT 6 AS INPT FROM DUAL UNION SELECT 7 AS INPT FROM DUAL UNION SELECT 9 AS INPT FROM DUAL UNION SELECT 10 AS INPT FROM DUAL UNION SELECT 11 AS INPT FROM DUAL ) SELECT INPT FROM ( SELECT INPT, DECODE(LEAD(INPT) OVER(ORDER BY INPT ASC) - 1, INPT, 1, 0) AS MINC, DECODE(LAG(INPT) OVER(ORDER BY INPT ASC) + 1, INPT, 1, 0) AS MAXC FROM T ) WHERE NOT(MINC = 1 AND MAXC = 1)
WITH T AS ( SELECT 1 AS INPT FROM DUAL UNION SELECT 2 AS INPT FROM DUAL UNION SELECT 3 AS INPT FROM DUAL UNION SELECT 6 AS INPT FROM DUAL UNION SELECT 7 AS INPT FROM DUAL UNION SELECT 9 AS INPT FROM DUAL UNION SELECT 10 AS INPT FROM DUAL UNION SELECT 11 AS INPT FROM DUAL ) SELECT MAX(MINV) AS MINV, MAX(MAXV) AS MAXV FROM ( SELECT ROUND(ROWNUM / 2) AS GID, DECODE(MINC, 1, INPT, 0) AS MINV, DECODE(MAXC, 1, INPT, 0) AS MAXV FROM ( SELECT INPT, DECODE(LEAD(INPT) OVER(ORDER BY INPT ASC) - 1, INPT, 1, 0) AS MINC, DECODE(LAG(INPT) OVER(ORDER BY INPT ASC) + 1, INPT, 1, 0) AS MAXC FROM T ORDER BY INPT ) WHERE NOT(MINC = 1 AND MAXC = 1) ) GROUP BY GID
--UNION --SELECT 17 AS INPT FROM DUAL -- --이런 데이타가 추가 되면 --이것은 최소 값으로도 볼 수 없고, 최대 값으로도 볼 수 없기 때문에 --이런 데이타는 삭제 처리가 필요 할 것 같습니다. WITH T AS ( SELECT 1 AS INPT FROM DUAL UNION SELECT 2 AS INPT FROM DUAL UNION SELECT 3 AS INPT FROM DUAL UNION SELECT 6 AS INPT FROM DUAL UNION SELECT 7 AS INPT FROM DUAL UNION SELECT 9 AS INPT FROM DUAL UNION SELECT 10 AS INPT FROM DUAL UNION SELECT 11 AS INPT FROM DUAL UNION SELECT 16 AS INPT FROM DUAL ) SELECT MAX(MINV) AS MINV, MAX(MAXV) AS MAXV FROM ( SELECT ROUND(ROWNUM / 2) AS GID, DECODE(MINC, 1, INPT, 0) AS MINV, DECODE(MAXC, 1, INPT, 0) AS MAXV FROM ( SELECT INPT, DECODE(LEAD(INPT) OVER(ORDER BY INPT ASC) - 1, INPT, 1, 0) AS MINC, DECODE(LAG(INPT) OVER(ORDER BY INPT ASC) + 1, INPT, 1, 0) AS MAXC FROM T ORDER BY INPT ) WHERE NOT(MINC = 1 AND MAXC = 1) OR (MINC = 0 AND MAXC = 0) ) WHERE MINV != 0 OR MAXV != 0 GROUP BY GID ORDER BY GID ;