简体   繁体   中英

finding consecutive numbers

ok - so i searched the internet for this, and none of the examples i found are exactly like mine.

i have a table with 5 columns and thousands of rows. i need to find consecutive numbers within each row. i need to end up with 3 queries for the situations shown below

n1   n2   n3   n4   n5
=======================
 1     3    4    6    9   = should result in 1 (when checking for pairs)
 1     3    4    5    9   = should result in 1 (when checking for triplets)
 1     2    5    8    9   = should result in 1 (when checking for double pairs)

This is what i have to move the columns into rows, but i am not sure how to check this now.

select n1 from (
select n1 from myTable where Id  = 1
union all select n2 from myTable where Id = 1
union all select n3 from myTable where Id = 1
union all select n4 from myTable where Id = 1
union all select n5 from myTable where Id = 1
) t
order by n1

Thank you for all your help!

@TimBiegeleise, update : so i found this on google for Gaps & Islands:

SELECT ID, StartSeqNo=MIN(SeqNo), EndSeqNo=MAX(SeqNo)
FROM (
SELECT ID, SeqNo
    ,rn=SeqNo-ROW_NUMBER() OVER (PARTITION BY ID ORDER BY SeqNo)
FROM dbo.GapsIslands) a
GROUP BY ID, rn;

this is my updated query converting the columns to rows (but it requires 2 statements, i much rather have 1) and implementing the island part - but i don't understand how that give me the result what i need (see above). below i show the original row data and the result.

select n1, IDENTITY (INT, 1, 1) AS ID 
into #test
from (
select n1 from myTable where Id  = 8
union all select n2 from myTable where Id = 8
union all select n3 from myTable where Id = 8
union all select n4 from myTable where Id = 8
union all select n5 from myTable where Id = 8
) as t
order by n1

SELECT ID, StartSeqNo=MIN(n1), EndSeqNo=MAX(n1)
FROM (
SELECT ID, n1
    ,rn=n1-ROW_NUMBER() OVER (PARTITION BY ID ORDER BY n1)
FROM #test) a
GROUP BY ID, rn

drop table #test

original row - should return 1 (when checking for "pair"/consecutive numbers
n1   n2   n3   n4   n5
=======================
31   27   28   36   12

the result i get with the above query:

    StartSeqNo  EndSeqNo
1   12          12
2   27          27
3   28          28
4   31          31
5   36          36

help :-) !

ok, i got it. this query returns a value of 1 for the above stated row

select COUNT(*) as pairs 
from (
SELECT StartSeqNo=MIN(n1), EndSeqNo=MAX(n1)
    FROM (
        SELECT n1, rn=n1-ROW_NUMBER() OVER (ORDER BY n1)
            from (
                select n1 from myTable where Id  = 8
                union all select n2 from myTable where Id = 8
                union all select n3 from myTable where Id = 8
                union all select n4 from myTable where Id = 8
                union all select n5 from myTable where Id = 8
            ) t
    ) x
GROUP BY rn
) z
where StartSeqNo+1 = EndSeqNo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM