我有一些像这样的桌子
row chequeNo
1 15
2 19
3 20
4 35
5 16
Run Code Online (Sandbox Code Playgroud)
我需要得到这样的结果
row from to
1 15 16
2 19 20
3 35 35
Run Code Online (Sandbox Code Playgroud)
所以我需要一组chequeNo值,这些值将是连续的,没有任何中断.chequeNo是独特的专栏.另外,它应该使用一个sql select查询,因为我没有权限创建除select查询之外的任何sql结构.
那有可能吗?
将不胜感激任何帮助
Rob*_*ijk 22
您可以在这里使用Aketi Jyuuzou的技术Tabibitosan:
SQL> create table mytable (id,chequeno)
2 as
3 select 1, 15 from dual union all
4 select 2, 19 from dual union all
5 select 3, 20 from dual union all
6 select 4, 35 from dual union all
7 select 5, 16 from dual
8 /
Table created.
SQL> with tabibitosan as
2 ( select chequeno
3 , chequeno - row_number() over (order by chequeno) grp
4 from mytable
5 )
6 select row_number() over (order by grp) "row"
7 , min(chequeno) "from"
8 , max(chequeno) "to"
9 from tabibitosan
10 group by grp
11 /
row from to
---------- ---------- ----------
1 15 16
2 19 20
3 35 35
3 rows selected.
Run Code Online (Sandbox Code Playgroud)
问候,
Rob.
小智 2
这应该适用于 Oracle 10(仅在 Oracle 11 上进行了测试)
select group_nr + 1,
min(chequeno) as start_value,
max(chequeno) as end_value
from (
select chequeno,
sum(group_change_flag) over (order by rn) as group_nr
from (
select row_number() over (order by chequeno) as rn,
chequeno,
case
when chequeno - lag(chequeno,1,chequeno) over (order by chequeno) <= 1 then 0
else 1
end as group_change_flag
from foo
) t1
) t2
group by group_nr
order by group_nr
Run Code Online (Sandbox Code Playgroud)
(它应该与任何支持标准 SQL 窗口函数的 DBMS 一起使用,例如 PostgreSQL、DB2、SQL Server 2012)
| 归档时间: |
|
| 查看次数: |
679 次 |
| 最近记录: |