我有一个像下面的桌子

从上表中,我需要基于“数字”列从val3列中选择相邻的值

我需要如下输出:

最佳答案

WITH T1
     AS (SELECT *,
                DENSE_RANK() OVER (PARTITION BY [Number] ORDER BY [Val3]) - [Val3] AS Grp
         FROM   YourTable),
     T2
     AS (SELECT *,
                COUNT(*) OVER (PARTITION BY [Number], Grp) AS Cnt
         FROM   T1)
SELECT [Number],
       [Val1],
       [Val2],
       [Val3]
FROM   T2
WHERE  Cnt > 1

关于sql - 如何在Sql中基于另一个列值查找列中的相邻值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20544758/

10-11 02:18