用于列线程关系的SQL查询

时间:2013-11-07 14:56:30

标签: sql tsql

这是表格的简化视图。我道歉,但我无法保存桌子的图片所以我希望这没关系。

  

C1 ___ C2
  1 ____一个
  1 ____ b
  2 ____一个
  2 ____ b
  2 ____
  2 ____ d
  3 ____Ë
  3 ____一个
  4 ____ž
  5 ____ d

结果是由于C2列的关系, 第1组将包括1,2,3,5(因为它们具有重叠的c2值,基本上表示a = b = c = d = e) 第2组将包括4

我有数百万行使用这种数据,目前有一个游标作业运行x次以构建这些组。我能够看到它应该如何工作,但我无法构建一个可以解决这种关系的查询。 有什么建议? 谢谢

1 个答案:

答案 0 :(得分:0)

在SQL Server 2012上测试:

WITH t AS (
    SELECT
        t.c1,
        t.c2,
        tm.c1_min
    FROM
        Test t
    JOIN
        (
            SELECT
                c2,
                MIN(c1) AS c1_min
            FROM
                Test
            GROUP BY
                c2
        ) AS tm
    ON
        t.c2 = tm.c2
),
rt AS (
    SELECT
        c1_min,
        c1,
        1 AS cnt
    FROM
        t
UNION ALL
    SELECT
        rt.c1_min,
        t.c1,
        rt.cnt + 1 AS cnt
    FROM
        rt
    JOIN
        t
    ON
        rt.c1 = t.c1_min
    AND
        rt.c1 < t.c1
)
SELECT
    SUM(t.rst) OVER (ORDER BY t.ord ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS group_number,
    t.c1
FROM
    (
        SELECT
            t.c1,
            t.rst,
            t.ord
        FROM
            (
                SELECT
                    rt.c1,
                    CASE
                        WHEN rt.c1_min = MIN(rt.c1_min) OVER (ORDER BY rt.c1_min, rt.c1 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) THEN 0
                        ELSE 1
                    END AS rst,
                    ROW_NUMBER() OVER (ORDER BY rt.c1_min, rt.c1) AS ord,
                    ROW_NUMBER() OVER (PARTITION BY rt.c1 ORDER BY rt.c1_min, rt.cnt) AS qfy
                FROM
                    rt
            ) AS t
        WHERE
            t.qfy = 1
    ) AS t;