select
min(col_1) as start_col_1,
case when max(col_1) <> min(col_1) then max(col_1) end as end_col_1,
col2
from (
select t.*,
row_number() over(partition by col2 order by col_1) as rn
from mytable t
) t
where col_2 = 3
group by col2, col_1 - rn
order by start_col1
select
min(col_1) as start_col_1,
case when max(col_1) <> min(col_1) then max(col_1) end as end_col_1,
col2
from (
select t.*,
row_number() over(order by col_1) as rn1,
row_number() over(partition by col2 order by col_1) as rn2
from mytable t
) t
where col_2 = 3
group by col2, rn1 - rn2
order by start_col1
1条答案
按热度按时间zwghvu4y1#
这是一个缺口和孤岛问题。下面是一种使用行号之间的差异来标识组的方法:
这是回报
null
而不是'-'
当孤岛只由一个记录组成时(这是因为后者不是有效的数字)。只要
col_1
无间隙增量。否则,我们可以用另一个生成自己的序列row_number()
: