我有一个名为“ phone_number_list”的表,该表具有id,phone_number,org_id和created_at。我正在尝试运行一个查询,该查询将删除表中每个org_id的重复的phone_number。谁能帮我这个?我知道如何使用MySQL进行此操作,但是第一次使用Postgresql。
答案 0 :(得分:0)
尝试一下:
delete from your_table where id in
(select max(id) from your_table
group by org_id, phone_number
having count(1) > 1);
答案 1 :(得分:0)
这是使用“窗口函数”的解决方案:
DELETE FROM phone_number_list
WHERE id IN
(SELECT id FROM
( SELECT id, ROW_NUMBER() OVER
(partition BY phone_number, org_id ORDER BY id ASC) AS rnum
FROM phone_number_list
) AS t WHERE t.rnum > 1);