如何比较多行
How to compare multiple rows
我有一个 table 的数据如下,想要 return 那些 group_id 具有独特的数据。 group_id 3 和 4 都有两个分量 123 和 456,所以它们是“重复的”,我们只需要 return 较小的 group_id,即 3。还有 group_id 5没有重复,可以 returned。所以我们希望 group_id 3 和 5 被 returned.
如何针对 postgres 数据库编写 SQL 查询来实现该目的?谢谢!
id
group_id
component_id
1
3
123
2
3
456
3
4
123
4
4
456
5
5
123
SELECT group_id, MIN(component_id)
FROM MyTable
GROUP BY group_id
HAVING COUNT(*) > 1
这是一种将 group_id 分配给 component_id 的方法。
它使用带数组的递归 CTE 来查找可能的组合。
递归从孤独的group_id开始。
然后下一个 CTE 选择最长的组合之一。
WITH RECURSIVE RCTE AS (
SELECT id, group_id, component_id
, 1 as Lvl
, array[group_id] as group_ids
, array[component_id] as component_ids
FROM YourTable
WHERE group_id IN (
SELECT group_id
FROM YourTable
GROUP BY group_id
HAVING COUNT(*) = 1
)
UNION ALL
SELECT t.id, t.group_id, t.component_id
, Lvl+1
, cte.group_ids || t.group_id
, cte.component_ids || t.component_id
FROM RCTE cte
JOIN YourTable t
ON t.group_id != ALL(group_ids)
AND t.component_id != ALL(component_ids)
)
, CTE_ARRAYS AS (
SELECT group_ids, component_ids
FROM RCTE
ORDER BY array_length(group_ids, 1) desc, Lvl desc
LIMIT 1
)
SELECT a.group_id, a.component_id
FROM CTE_ARRAYS c
CROSS JOIN LATERAL UNNEST(c.group_ids, c.component_ids) WITH ORDINALITY AS a(group_id, component_id)
ORDER BY a.group_id;
group_id
component_id
3
456
5
123
db<>fiddle here
使用 2 级聚合:
SELECT MIN(group_id) group_id
FROM (
SELECT group_id, STRING_AGG(component_id::text, ',' ORDER BY component_id) components
FROM tablename
GROUP BY group_id
) t
GROUP BY components;
参见demo。
我有一个 table 的数据如下,想要 return 那些 group_id 具有独特的数据。 group_id 3 和 4 都有两个分量 123 和 456,所以它们是“重复的”,我们只需要 return 较小的 group_id,即 3。还有 group_id 5没有重复,可以 returned。所以我们希望 group_id 3 和 5 被 returned.
如何针对 postgres 数据库编写 SQL 查询来实现该目的?谢谢!
id | group_id | component_id |
---|---|---|
1 | 3 | 123 |
2 | 3 | 456 |
3 | 4 | 123 |
4 | 4 | 456 |
5 | 5 | 123 |
SELECT group_id, MIN(component_id)
FROM MyTable
GROUP BY group_id
HAVING COUNT(*) > 1
这是一种将 group_id 分配给 component_id 的方法。
它使用带数组的递归 CTE 来查找可能的组合。
递归从孤独的group_id开始。
然后下一个 CTE 选择最长的组合之一。
WITH RECURSIVE RCTE AS ( SELECT id, group_id, component_id , 1 as Lvl , array[group_id] as group_ids , array[component_id] as component_ids FROM YourTable WHERE group_id IN ( SELECT group_id FROM YourTable GROUP BY group_id HAVING COUNT(*) = 1 ) UNION ALL SELECT t.id, t.group_id, t.component_id , Lvl+1 , cte.group_ids || t.group_id , cte.component_ids || t.component_id FROM RCTE cte JOIN YourTable t ON t.group_id != ALL(group_ids) AND t.component_id != ALL(component_ids) ) , CTE_ARRAYS AS ( SELECT group_ids, component_ids FROM RCTE ORDER BY array_length(group_ids, 1) desc, Lvl desc LIMIT 1 ) SELECT a.group_id, a.component_id FROM CTE_ARRAYS c CROSS JOIN LATERAL UNNEST(c.group_ids, c.component_ids) WITH ORDINALITY AS a(group_id, component_id) ORDER BY a.group_id;
group_id | component_id |
---|---|
3 | 456 |
5 | 123 |
db<>fiddle here
使用 2 级聚合:
SELECT MIN(group_id) group_id
FROM (
SELECT group_id, STRING_AGG(component_id::text, ',' ORDER BY component_id) components
FROM tablename
GROUP BY group_id
) t
GROUP BY components;
参见demo。