-5

This my original table:

enter image description here

Intended table:

enter image description here

I can't add a new column to table.

doğukan
  • 23,073
  • 13
  • 57
  • 69

3 Answers3

5

You can delete duplicates using i.e. ROW_NUMBER():

with duplicates as
(
    select
        *
        ,ROW_NUMBER() OVER (PARTITION BY FirstName, LastName, age ORDER BY FirstName) AS number
    from yourTable
)
delete 
from duplicates
where number > 1

Each row where number is bigger than 1 is a duplicate.

M. Kanarkowski
  • 2,155
  • 8
  • 14
  • 3
    careful, you didn't partition by `Age` as well which **is needed** to remove true *duplicates* – S3S May 20 '19 at 20:50
4

You can use a common table expression to delete all but the first record in a set of duplicates:

with cte as (
    select *, row_number()
     over (partition by FirstName, LastName, Age order by FirstName, LastName, Age) as row_number
    from tbl
)
delete from cte where row_number <> 1
jspcal
  • 50,847
  • 7
  • 72
  • 76
1

You need some way of identifying the rows in the absence of a PK.

The only way I can think of is to:

  1. Retrieve the "Row IDs" of the rows,
  2. Then pick one as the "good one" for every non-unique row,
  3. And finally delete all the other ones.

I'm not positive about this solution, though. I think this answer can help to produce distinctive row ids. I hope it helps.

The Impaler
  • 45,731
  • 9
  • 39
  • 76