-4

This my original table:

enter image description here

Intended table:

enter image description here

I can't add a new column to table.

3
  • 4
    How do you decide which ones are the "good ones" and which ones are the "bad ones"? Commented May 20, 2019 at 20:43
  • 5
    I think the main issue here originates in the absence of a primary key. Commented May 20, 2019 at 20:47
  • 1
    SQL Server questions need to show an attempt Commented May 20, 2019 at 20:48

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

careful, you didn't partition by Age as well which is needed to remove true duplicates
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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.