根据记录进行连续号的分档显示
/*
测试环境:windows 2K server + Mssql 2000
所有功能都进行测试过,并有相应的结果集,如果有什么疑义在论坛跟帖
关于版权的说明:部分资料来自互联网,如有不当请联系版主,版主会在第一时间处理。
功能:根据记录进行连续号的分档显示。
比如有如下结果集
代码 编号
---- --------
a F001
a F002
a F003
a F005
a F008
a F009
a F010
a F030
要想生成如下的结果集
代码 起始号 截止号
---- -------- --------
a F001 F003
a F005 F005
a F008 F010
a F030 F030
*/
--SQL
select
a.代码,
起始号=a.编号,
截止号=min(b.编号)
from
(select t.* from @a t where not exists(select 1 from @a where 代码=t.代码 and right(编号,3)=right(t.编号,3)-1)) a,
(select t.* from @a t where not exists(select 1 from @a where 代码=t.代码 and right(编号,3)=right(t.编号,3)+1)) b
where
a.代码=b.代码 and a.编号<=b.编号
group by
a.代码,a.编号
/*
代码 起始号 截止号
---- -------- --------
a F001 F003
a F005 F005
a F008 F010
a F030 F030
(所影响的行数为 4 行)
*/