Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicates using partition by SQL Server

I need to remove duplicates from a table:

;WITH cte as(
SELECT ROW_NUMBER() OVER (PARTITION BY [specimen id]
                                       ORDER BY ( SELECT 0 ) ) RN
         FROM   quicklabdump)
        delete from cte where RN>1

The column quicklabdumpID is the primary key.

I would like to know how to keep only the largest quicklabdumpID where there are multiple occurrences of [specimen id]

like image 434
Alex Gordon Avatar asked Feb 03 '12 01:02

Alex Gordon


People also ask

How do I remove duplicates in SQL Server?

To delete the duplicate rows from the table in SQL Server, you follow these steps: Find duplicate rows using GROUP BY clause or ROW_NUMBER() function. Use DELETE statement to remove the duplicate rows.

How can I delete duplicate records in SQL using distinct?

SELECT ALL columns FROM table; The syntax diagram is: SELECT [ALL | DISTINCT] columns FROM table; If a table has a properly defined primary key, SELECT DISTINCT * FROM table; and SELECT * FROM table; return identical results because all rows are unique.


1 Answers

Change your order by to quicklabdumpid DESC.

WITH cte as(
  SELECT ROW_NUMBER() OVER (PARTITION BY [specimen id]
                            ORDER BY  quicklabdumpid DESC ) RN
  FROM   quicklabdump)
delete from cte where RN>1
like image 88
Mikael Eriksson Avatar answered Oct 20 '22 19:10

Mikael Eriksson