简体   繁体   中英

How to delete the duplicates from the table using row_number() in psql?

I have written the following query, i am using greenplum db and dbeaver for implementation.

with cte as 
(select 
*, 
row_number() over(partition by first_name order by roll_num) row_num 
from table_name 
where roll_num in ('0011')) 
delete from cte where row_num>1;

The above query is returning error. Can someone help me here, please!

Please try like this it works for me:

select *  from tablename as t 
where exists 
      ( select * 
        from tablename as d 
        where d.ctid > t.ctid 
          and d.* is not distinct from t.*
      ) ;

delete from tablename as t 
    where exists 
          ( select * 
            from tablename as d 
            where d.ctid > t.ctid 
              and d.* is not distinct from t.*
          ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM