PCB MS SQL 排序应用---SQL相邻数据区间值求解

时间:2023-03-10 02:47:33
PCB MS SQL 排序应用---SQL相邻数据区间值求解

其中一篇 博文中有写《PCB MS SQL 排序应用---相邻数据且相同合并处理》此篇有也应相用也同的技巧,实现相邻数据区间值求解

示例:

原数据:处理前

PCB MS SQL 排序应用---SQL相邻数据区间值求解

求出区间值:处理后

PCB MS SQL 排序应用---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)