postgresql delete duplicated rows

Deleting duplicates

使用window function row_number 对数据partition

举例:

isnp=# create table student (id serial, name text, age int);
isnp=# \d student;
isnp=# with cte as (select *, round(random()*100) as d from generate_series(1,10000) as r) insert into student (name, age)  select 'lmy'||r, d from cte;
### genetate duplicate row
isnp=# insert into student (name, age) select name, age from student where 100<id and id<200; 
isnp=# with xxx (id, name, age, rn)  as (select *, row_number() over(partition by name order by id) rn from student) delete from student where id in (select id from xxx where rn > 1);
    原文作者:dreambei
    原文地址: https://segmentfault.com/a/1190000006966930
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞