如何从该数据集中创建组?

How can I create groups out of this data set?

我有一个table如下:

   id    | similar_id|
 --------+-----------+
    1    |     2     |
    1    |     3     |
    2    |     1     |
    2    |     3     |
    3    |     1     |
    3    |     2     |
    4    |     5     |
    5    |     4     |

如何编写优化的 sql 查询以便找到相似的组?
即组 1 -> {1,2,3},组 2 -> {4,5}


P.S.,我知道数据本来可以组织得更好。

select distinct (
    select array_agg(a order by a)
    from unnest(a) a(a)
) as "group"
from (
    select id || array_agg(similar_id) a
    from t
    group by id
) s
order by 1
;
  group  
---------
 {1,2,3}
 {4,5}

来自这个数据:

create table t (id int, similar_id int);
insert into t (id, similar_id) values
(1,2),
(1,3),
(2,1),
(2,3),
(3,1),
(3,2),
(4,5),
(5,4);

简单明了SQL:

SELECT DISTINCT groups
FROM (
  SELECT id, array_agg(similar_id) groups
  FROM (
    SELECT id, similar_id
    FROM grp
    UNION
    SELECT DISTINCT id, id
    FROM grp
    ORDER BY 1, 2) subsub
  GROUP BY 1) sub
ORDER BY 1;

这将产生:

 groups 
---------
 {1,2,3} 
 {4,5} 

这里的技巧是使用 UNION 这样每个 id 都与自己配对并且它是 similar_ids 然后它只是一个聚合问题。