coperations07
asked on
Need to remove duplicated records that do have unique id
I put in an archive function a few days ago that had a join problem. I ended up with some duplicated records that I need to purge out. There is a unique ID field and the CreateDt is different for each record that is duplicated. There should be just one record for each sorter,wave,order,chute combo. The attached file shows 38 rows of sample data. There should only be 3 rows. What is the best way to save the earliest record and delete the others that were duplicated?
OrderData.xlsx
OrderData.xlsx
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys! I will give this a try.
ASKER
I've tried
SELECT ID, ROW_NUMBER() OVER (PARTITION BY SorterID, WaveID, OrderID, chute ORDER BY ID) AS Idx FROM db_sort00.dbo.tbl_arch_seq _header AS Idx
WHERE
Idx > 1
It is telling me Idx is an invalid column. I've tried a few different things to tweak it but haven't got it to work for me yet.
SELECT ID, ROW_NUMBER() OVER (PARTITION BY SorterID, WaveID, OrderID, chute ORDER BY ID) AS Idx FROM db_sort00.dbo.tbl_arch_seq
WHERE
Idx > 1
It is telling me Idx is an invalid column. I've tried a few different things to tweak it but haven't got it to work for me yet.
You have 2 things named "AS Idx".
Looking back, it looks like the typo originated in my previous comment. Sorry about that.
SELECT ID, ROW_NUMBER() OVER (PARTITION BY SorterID, WaveID, OrderID, chute ORDER BY ID) AS Idx FROM db_sort00.dbo.tbl_arch_seq_header
WHERE
Idx > 1
Looking back, it looks like the typo originated in my previous comment. Sorry about that.
ASKER
I've tried again today with the edit, but Idx is still not recognized as a column.
I was able to get this to return results. I haven't deleted anything yet though:
SELECT ID, (ROW_NUMBER() OVER (PARTITION BY SorterID, WaveID, OrderID, chute ORDER BY ID)) AS Idx
FROM db_sort00.dbo.tbl_arch_seq_header
WHERE Idx > 1
I was able to get this to return results. I haven't deleted anything yet though:
;WITH CTE
AS (
SELECT ID,
Row_number()OVER (PARTITION BY SorterID, WaveID, OrderID, chute ORDER BY (SELECT 1) ) AS Rn
FROM db_sort00.dbo.tbl_arch_seq_header)
SELECT *
FROM CTE
WHERE Rn > 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I accepted my own solution because it is what ended up working for me.
Open in new window