Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查找连续数_Sql_Sql Server - Fatal编程技术网

Sql 查找连续数

Sql 查找连续数,sql,sql-server,Sql,Sql Server,好的,我在网上搜索了一下,没有一个例子和我的完全一样 我有一个5列数千行的表格。 我需要在每行中找到连续的数字。我需要对下面所示的情况进行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 che

好的,我在网上搜索了一下,没有一个例子和我的完全一样

我有一个5列数千行的表格。 我需要在每行中找到连续的数字。我需要对下面所示的情况进行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)
这就是我必须将列移动到行中的内容,但我不确定现在如何检查它

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
谢谢你的帮助

@TimBiegeleise,更新: 所以我在谷歌的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;
这是我更新的查询,它将列转换为行(但它需要2条语句,我更愿意有1条语句)并实现孤岛部分——但我不明白这是如何得到我需要的结果的(见上文)。下面我将显示原始行数据和结果

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
我通过上述查询得到的结果:

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

帮助:-)

好的,我知道了。此查询为上述行返回值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

在一般情况下,这看起来像是一个缺口和孤岛问题,只是您的数据格式使得数据库在帮助方面毫无用处。您肯定应该将每个序列的数据存储在一列中,而不是跨一行中的列。谢谢您的回复。这也是我的想法,这也是发布的查询将要做的。它将为每一行创建一个包含1列的新临时表。结果是一列的数字在不同的行中。您能告诉我们您使用的是什么数据库吗?不同平台之间的SQL存在差异,这些平台提供了不同的功能,在这种情况下可能会有所帮助。很抱歉延迟。我正在使用SQL server(我想是2010年),但考虑稍后将此数据库迁移到SQLExpress@TimBiegeleisen,了解缺口和孤岛,这可能会奏效。但在这种情况下,我将如何应用/编码它呢?嗯,因此这对于1条记录(在本例中是ID为8的行)非常有效。但是我如何改变这个查询,使每行返回一个结果呢?有人有什么想法吗?