SQL正确实现方式:
/*分组统计各组最小开始值和最大结束值、连续次数*/
select 型号,concat('HT',min(RIGHT(序列号,len(序列号)-2))) as min_num,concat('HT',max(RIGHT(序列号,len(序列号)-2))) as max_num,
count(*) as 次数
from (
/*增加辅助列,利用序号和行号差值进行分组,注意必须使用开窗函数*/
select 序列号,型号,right(序列号,len(序列号)-2) as xh,
ROW_NUMBER() over (order by 序列号) as 行号,
convert(int,right(序列号,len(序列号)-2))- ROW_NUMBER() over (partition by 型号 order by 序列号) as 辅助列
from xs0b7p
) t
group by 型号,辅助列
having count(*)>=1
效果图:
