我无法弄清楚对此的查询,我发现的一切都是立即行不通的,因为" group by"方面。
ID | Username | Age | arbitrary
1 | bob | 10 | 34
2 | bob | 10 | 54
3 | alice | 10 | 123
ID 1和2都会被删除,并且不会有任何改变。我设法创建了一个类似这样的问题的网络应用,并希望应用一个唯一的(用户名,年龄)来修补它,但由于重复,我无法做到。
答案 0 :(得分:1)
DELETE FROM thetable dd
WHERE EXISTS (
SELECT *
FROM thetable xx
WHERE xx.username = dd.username
AND xx.age = dd.age
AND xx.id <> dd.id -- if you want to delete ALL duplicates
-- AND xx.id < dd.id -- if you want to keep only ONE of the duplicates
);
答案 1 :(得分:0)
绕过#34;&#34;方面你可以使用窗口函数:
with duplicates as (
select id,
count(*) over (partition by username, age) as dup_count
from the_table
)
select *
from duplicates
where dup_count > 1;
有关手册中窗口功能的更多信息:
http://www.postgresql.org/docs/current/static/tutorial-window.html
这可以与delete
声明结合使用:
with duplicates as (
select id,
count(*) over (partition by username, age) as dup_count
from the_table
)
delete from the_table
where id in (select id
from duplicates
where dup_count > 1);
以上使用数据修改CTE。有关详细信息,请参阅手册:
http://www.postgresql.org/docs/current/static/queries-with.html#QUERIES-WITH-MODIFYING
SQLFiddle示例:http://sqlfiddle.com/#!15/8b0e3/1
答案 2 :(得分:0)
DELETE FROM tablename
WHERE username IN (SELECT username
FROM (SELECT row_number() OVER (PARTITION BY username), username
FROM tablename) x
WHERE x.row_number > 1);
------RESULT------
ID | Username | Age | arbitrary
-----+---------------+-----+----------
3 | alice | 10 | 123