或者,使用LEAD分析函数以及您喜欢的格式。TESTCTE 是您已经拥有的;第 9 行开始就是您所需要的。
SQL> with test (col) as
2 (select 1 from dual union all
3 select 3 from dual union all
4 select 4 from dual union all
5 select 9 from dual union all
6 select 10 from dual union all
7 select 11 from dual
8 ),
9 temp as
10 (select col,
11 lead(col) over (order by col) lcol
12 from test
13 )
14 select '[' || col ||' - '|| lcol ||']' result
15 From temp
16 where lcol - col > 1
17 order by col;
RESULT
-------------------------------------------------------
[1 - 3]
[4 - 9]
SQL>
Run Code Online (Sandbox Code Playgroud)
[编辑:调整,这样你就不必考虑太多]
这就是你所拥有的:
SQL> select * From t_mark;
M_ID
----------
1
3
4
9
10
11
6 rows selected.
Run Code Online (Sandbox Code Playgroud)
这就是您所需要的:
SQL> with temp as
2 (select m_id,
3 lead(m_id) over (order by m_id) lm_id
4 from t_mark
5 )
6 select '[' || m_id ||' - '|| lm_id ||']' result
7 From temp
8 where lm_id - m_id > 1
9 order by m_id;
RESULT
------------------------------------------------------------------
[1 - 3]
[4 - 9]
SQL>
Run Code Online (Sandbox Code Playgroud)
基本上,您应该学习如何使用 CTE(公共表表达式,又名with Factoring 子句)。