给你个SQL语句供你参考,可以找出连续数字的开始和结束值。
with T as
(
select '1' as 编号 ,'2019-11-03' as 日期 union ALL
select '2' ,'2019-11-03' union ALL
select '3' ,'2019-11-03' union ALL
select '4' ,'2019-11-03' union ALL
select '10' ,'2019-11-03' union ALL
select '11' ,'2019-11-03' union ALL
select '12' ,'2019-11-03' union ALL
select '13' ,'2019-11-03' union ALL
select '14' ,'2019-11-03' union ALL
select '20' ,'2019-11-04' union ALL
select '21' ,'2019-11-04' union ALL
select '22' ,'2019-11-04' union ALL
select '23' ,'2019-11-04' union ALL
select '31' ,'2019-11-04' union ALL
select '32' ,'2019-11-04' union ALL
select '33' ,'2019-11-04' union ALL
select '34' ,'2019-11-04'
),
tt
as
(
select *,
ROW_NUMBER() over(partition by 日期 order by cast(编号 as int)) as rownum
from T
)
select 日期,
MIN(cast(编号 as int)) as 起号 ,
max(cast(编号 as int)) as 止号
from tt
group by 日期,编号- rownum
/*
日期 起号 止号
2019-11-03 1 4
2019-11-03 10 14
2019-11-04 20 23
2019-11-04 31 34
*/
总结方法就是:
1、给原始数据增加一个分组(按日期)排序字段rownum。
2、然后再根据
日期,编号- rownum进行分组,取最大值和最小值。
连续数字时,编号- rownum的值是相同的,方法的关键点就在这了。