Вижте този пример
WITH ORDH
AS (SELECT 1111 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1112 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1115 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1116 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1118 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1121 AS ORDERNO FROM DUAL),
GOT_NEXT_ORDERNO
AS (SELECT ORDERNO,
LEAD ( ORDERNO ) OVER (ORDER BY ORDERNO) AS NEXT_ORDERNO
FROM ORDH)
SELECT ORDERNO + 1 AS FROM_NO, NEXT_ORDERNO - 1 AS TO_NO
FROM GOT_NEXT_ORDERNO
WHERE ORDERNO + 1 <> NEXT_ORDERNO;
FROM_NO TO_NO
---------- ----------
1113 1114
1117 1117
1119 1120
3 rows selected.