简体   繁体   English

寻找连续的数字

[英]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. 我有一个5列和数千行的表。 i need to find consecutive numbers within each row. 我需要在每一行中找到连续的数字。 i need to end up with 3 queries for the situations shown below 我需要对以下情况进行3个查询

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: @TimBiegeleise,更新:所以我在谷歌上找到了关于差距和岛屿的信息:

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). 这是我更新的查询,将列转换为行(但是它需要2条语句,我宁愿有1条语句)并实现孤岛部分-但我不知道该怎么给我所需的结果(见上文)。 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 该查询为上述行返回值1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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