Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find missing sequence number which is minimum

Tags:

sql

sql-server

I have one table which has sequence number.

I want to find the missing sequence number which is minimum out of missing numbers.

For eg.,

Seq No: 1,2,3,5,7,9 ==> Expected result: 4
Seq No: 1,2,3,4,5,6,7,9,15 ==> Expected result: 8
like image 676
Srinivasan Avatar asked Nov 25 '25 02:11

Srinivasan


1 Answers

You can try with row_number()

with cte as
(
select seq,seq-row_number() over(order by seq) as diff
from tablename
)
select * from cte where diff<>0 and diff in (select min(diff) from cte where diff<>0)
like image 130
Fahmi Avatar answered Nov 27 '25 15:11

Fahmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!