我有一个选择查询,它将返回类似下表的内容:
start | stop | id ------------------ 0 | 100 | 1 1 | 101 | 1 2 | 102 | 1 2 | 102 | 2 5 | 105 | 1 7 | 107 | 2 ... 300 | 400 | 1 370 | 470 | 1 450 | 550 | 1
其中stop = start + n;在这种情况下,n = 100。
我想合并每个id的重叠:
start | stop | id ------------------ 0 | 105 | 1 2 | 107 | 2 ... 300 | 550 | 1
id 1不给0 - 550,因为开始300在停止105之后。
第一个查询将返回数十万条记录,n最多可达数万条,因此处理得越快越好。
使用PostgreSQL btw。
答案 0 :(得分:2)
WITH bounds AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY start) AS rn
FROM (
SELECT id, LAG(stop) OVER (PARTITION BY id ORDER BY start) AS pstop, start
FROM q
UNION ALL
SELECT id, MAX(stop), NULL
FROM q
GROUP BY
id
) q2
WHERE start > pstop OR pstop IS NULL OR start IS NULL
)
SELECT b2.start, b1.pstop
FROM bounds b1
JOIN bounds b2
ON b1.id = b2.id
AND b1.rn = b2.rn + 1