Postgresql - 消除重复条目

Postgresql - eliminate duplicate entries

我关注Table:

+-----+-----+
| id1 | id2 |
+-----+-----+
| 1   | 2   |
| 1   | 3   |
| 2   | 1   |
| 2   | 3   |
| 5   | 1   |
| 5   | 2   |
+-----+-----+

我不想得到像

这样的重复条目
1 | 2
2 | 1

最后我需要以下内容:

+-----+-----+
| id1 | id2 |
+-----+-----+
| 1   | 2   |
| 1   | 3   |
| 2   | 3   |
| 5   | 1   |
| 5   | 2   |
+-----+-----+

执行此操作的最佳解决方案是什么?

这个怎么样:

DELETE FROM table WHERE (id1, id2) IN 
(SELECT t1.id1, t1.id2 FROM table t1 INNER JOIN table t2 ON t1.id1 = t2.id2 AND t1.id2 = t2.id1);

编辑:

哦,我以为你想把它们都删除。

DELETE FROM table WHERE (id1, id2) NOT IN(
SELECT least(id1, id2), greatest(id1, id2)
FROM table);

SELECT DISTINCT least(id1, id2), greatest(id1, id2)
FROM table

我自己找到了解决办法。我想,我的问题措辞不当。

SELECT DISTINCT
    CASE WHEN u.id1 > u.id2 THEN u.id2
        ELSE u.id1
    END as id1,
    CASE WHEN u.id1 < u.id2 THEN u.id2
        ELSE u.id1
    END as id2
FROM table u

我将较小的值排到了左边。
DISTINCT 消除重复项。

如果每对最多可以有两个条目,则:

select id1, id2
from t
where id1 < id2
union all
select id1, id2
from t
where id2 > id1 and
      not exists (select 1 from t t2 where t2.id1 = t.id2 and t2.id2 = t.id1);

如果你只想要独特的对,我会使用 least()greatest():

select least(id1, id2), greatest(id1, id2)
from t
group by least(id1, id2), greatest(id1, id2);

编辑:

您也可以使用 window 函数或 distinct on:

select distinct on (least(id1, id2), greatest(id1, id2)) t.*
from t
order by least(id1, id2), greatest(id1, id2);