SQL查询连续号码段

本文介绍了一种使用SQL查询连续号码段的方法。通过创建测试数据表并运用自连接、窗口函数等技术,实现了对号码段进行有效识别及汇总。适用于数据库管理与维护人员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL查询连续号码段

--测试数据

create table stest
(
 fphm int,
 kshm nvarchar(10)
)

insert into stest
select 2014,'00000001'
union all
select 2014,'00000002'
union all
select 2014,'00000003'
union all
select 2014,'00000004'
union all
select 2014,'00000005'
union all
select 2014,'00000007'
union all
select 2014,'00000008'
union all
select 2014,'00000009'
union all
select 2013,'00000120'
union all
select 2013,'00000121'
union all
select 2013,'00000122'
union all
select 2013,'00000124'
union all
select 2013,'00000125'
--执行语句
select fphm,min(kshm),max(kshm)
from
(
select fphm,kshm,cast(kshm as int)-cc as s
from
(
select fphm,kshm,row_number() over(order by fphm) as cc
from stest
) xx
) b group by b.fphm,b.s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值