其中一篇 博文中有写《PCB MS SQL 排序应用---相邻数据且相同合并处理》此篇有也应相用也同的技巧,实现相邻数据区间值求解
示例:
原数据:处理前
求出区间值:处理后
SQL 代码实现
DECLARE @table table(RowNo INT,FlowTechName VARCHAR(20)) INSERT INTO @table(RowNo,FlowTechName) VALUES(1,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VALUES(4,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(5,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(6,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(7,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VALUES(10,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VALUES(11,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VALUES(15,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(18,'沉铜') INSERT INTO @table(RowNo,FlowTechName) VALUES(20,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(21,'钻孔') INSERT INTO @table(RowNo,FlowTechName) VALUES(22,'钻孔') SELECT * FROM @table SELECT min(RowNo) minRank,MAX(RowNo) maxRank,FlowTechName FROM (SELECT FlowTechName,RowNo,row_number()over(partition by FlowTechName order by RowNo) -RowNo DiffVal FROM @table) t GROUP BY FlowTechName,DiffVal ORDER BY MAX(RowNo)