sql题---找到连续区间的开始和结束数字
1. 找到连续区间的开始和结束数字
Logs 表:
+------------+
| log_id |
+------------+
| 1 |
| 2 |
| 3 |
| 7 |
| 8 |
| 10 |
+------------+
结果表:
+------------+--------------+
| start_id | end_id |
+------------+--------------+
| 1 | 3 |
| 7 | 8 |
| 10 | 10 |
+------------+--------------+
2. 问题解决,用到了分区函数
select
min(log_id) start_id,
max(log_id) end_id
from
(
select
log_id,
log_id - row_number() over() as diff
from Logs
) as t
group by diff
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/16040827.html