PostgreSQL Removing duplicates

后端 未结 4 1774
不思量自难忘°
不思量自难忘° 2021-01-18 12:33

I am working on postgres query to remove duplicates from a table. The following table is dynamically generated and I want to write a select query which will remove the recor

4条回答
  •  感动是毒
    2021-01-18 12:39

                 select count(first) as cnt, first, second 
                 from df1 
                 group by first
                 having(count(first) = 1)
    

    if you want to keep one of the rows (sorry, I initially missed it if you wanted that):

                 select first, min(second) 
                 from df1 
                 group by first
    

    Where the table's name is df1 and the columns are named first and second.

    You can actually leave off the count(first) as cnt if you want.

    At the risk of stating the obvious, once you know how to select the data you want (or don't want) the delete the records any of a dozen ways is simple.

    If you want to replace the table or make a new table you can just use create table as for the deletion:

                 create table tmp as 
                 select count(first) as cnt, first, second 
                 from df1 
                 group by first
                 having(count(first) = 1);
    
                 drop table df1;
    
                 create table df1 as select * from tmp;
    

    or using DELETE FROM:

    DELETE FROM df1 WHERE first NOT IN (SELECT first FROM tmp);
    

    You could also use select into, etc, etc.

提交回复
热议问题