1

I read previous questions but they are not similar to mine (example this has no unique identifier while I have) .

say this are my tables rows:

  id     string       x    y    z    time_x                 
10318    'hello'   33700  5053  8  2015-07-03 12:39:49.61408
14071    'hello'   33700  5053  8  2015-07-03 12:39:49.61408

basically everything is the same except the id of the row. How can I identify these cases and delete the redundant rows?

Note: not all rows in table are duplicated. Regular rows should be left as is.

Community
  • 1
  • 1
quack
  • 353
  • 2
  • 4
  • 18

2 Answers2

5

Let say that table name is tbl

Delete from tbl where id not in (select min(id) from tbl group by string, x, y, z)

artm
  • 8,554
  • 3
  • 26
  • 43
dabal
  • 410
  • 3
  • 15
0

This query uses window function to find duplicated rows. It leaves the row with smallest id:

delete from foo_table where id in (
    select dupid from (
        SELECT lead(id) over (partition by string, x, y, z, time_x order by id) dupid
        from foo_table
    ) A where dupid is not null 
);
hruske
  • 2,205
  • 19
  • 27