This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input & output data, and if column forms a sequence then print ‘Yes’ other print ‘No’.
First input data in sequence:
1 2 3 4 5 6 7 8 |
n ----------- 100 101 102 103 104 105 |
Require Output:
1 2 3 |
Flag ---- Yes |
Create a table with sample data:
1 2 3 4 |
create table rno (n int) insert into rno values (100),(101),(102),(103),(104),(105) |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
with cteSeq as ( select n ,ROW_NUMBER() over (order by n) as rnk from rno ) ,cteCnt as ( select n-rnk as res ,COUNT(1) as cnt from cteSeq group by (n-rnk) ) select case when count(1) = 1 then 'Yes' else 'No' end as Flag from cteCnt |
Leave a Reply