求解 SQL字段连续性查询解决办法
求解 SQL字段连续性查询
如有表:
A B
301 1
301 2
301 3
301 5
301 6
301 7
301 9
301 13
301 14
如何查询得到b字段中第一个断了,不连续的最后一条的数据
A B
301 3
------解决方案--------------------
select A,max(B) as B from (
select A,B,row_number() over(partition by A order by B asc) as SortId from tablename
) as tbl where B =SortId group by A
------解决方案--------------------
select top 1 * from tableName as table1 where NOT EXISTS ( select * from tableName as table2 where table2.b = table1.b + 1 )
测试通过
------解决方案--------------------
这个就更好办了,假定这个表名为table1
SELECT A,MIN(B) FROM table1 t1 WHERE NOT EXISTS (SELECT * FROM table1 t2 WHERE t2.B = t1.B +1)
GROUP BY A
如有表:
A B
301 1
301 2
301 3
301 5
301 6
301 7
301 9
301 13
301 14
如何查询得到b字段中第一个断了,不连续的最后一条的数据
A B
301 3
------解决方案--------------------
select A,max(B) as B from (
select A,B,row_number() over(partition by A order by B asc) as SortId from tablename
) as tbl where B =SortId group by A
------解决方案--------------------
select top 1 * from tableName as table1 where NOT EXISTS ( select * from tableName as table2 where table2.b = table1.b + 1 )
测试通过
------解决方案--------------------
这个就更好办了,假定这个表名为table1
SELECT A,MIN(B) FROM table1 t1 WHERE NOT EXISTS (SELECT * FROM table1 t2 WHERE t2.B = t1.B +1)
GROUP BY A