这是表格的简化视图。我道歉,但我无法保存桌子的图片所以我希望这没关系。
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次以构建这些组。我能够看到它应该如何工作,但我无法构建一个可以解决这种关系的查询。 有什么建议? 谢谢
答案 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;