对具有完全相同子集的父项的SQL查询

时间:2014-03-20 06:27:59

标签: sql postgresql aggregate-functions relational-division

我的表中有父子关系(下面列出的简化版本):

| parent | child |
|--------+-------|
| p1     | c1    |
| p1     | c2    |
|--------+-------|
| p2     | c1    |
| p2     | c2    |
|--------+-------|
| p3     | c1    |
|--------+-------|
| p4     | c2    |
|--------+-------|
| p5     | c3    |

我需要向所有父母查询完全相同的子集,例如p1p2

似乎与this question有关,但有硬编码的子女。

1 个答案:

答案 0 :(得分:0)

此查询列出了共享相同子项的所有父项,其中有多个父项:

SELECT array_agg(parent) AS parents, children, count(*) AS dupes
FROM (
   SELECT parent, array_agg(child ORDER BY child) AS children
   FROM   tbl
   GROUP  BY 1
   ) sub
GROUP  BY 2
HAVING count(*) > 1;

简单但不是最快的解决方案。对于 big 表,我会尝试别的。

问题属于“关系师”家庭。这是我们前一段时间收集的查询样式的集合:
How to filter SQL results in a has-many-through relation

获得每行单亲的一种方式(很多):

WITH cte AS (
   SELECT parent, array_agg(child ORDER BY child) AS children
   FROM   tbl
   GROUP  BY 1
   )
SELECT *
FROM   cte c
WHERE EXISTS (
   SELECT 1
   FROM   cte c1
   WHERE  c1.children = c.children
   AND    c1.parent <> c.parent
   );