Deleting duplicates rows from redshift

后端 未结 7 2013
南方客
南方客 2020-12-31 02:45

I am trying to delete some duplicate data in my redshift table.

Below is my query:-

With duplicates
As
(Select *, ROW_NUMBER() Over (PARTITION by rec         


        
7条回答
  •  半阙折子戏
    2020-12-31 03:02

    Simple answer to this question:

    1. Firstly create a temporary table from the main table where value of row_number=1.
    2. Secondly delete all the rows from the main table on which we had duplicates.
    3. Then insert the values of temporary table into the main table.

    Queries:

    1. Temporary table

      select id,date into #temp_a from (select *
      from (select a.*, row_number() over(partition by id order by etl_createdon desc) as rn from table a where a.id between 59 and 75 and a.date = '2018-05-24') where rn =1)a

    2. deleting all the rows from the main table.

      delete from table a where a.id between 59 and 75 and a.date = '2018-05-24'

    3. inserting all values from temp table to main table

      insert into table a select * from #temp_a.

提交回复
热议问题