I have this code from this link: How can I remove duplicate rows?
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id, date_work, hours
ORDER BY ( SELECT 0)) RN
FROM work_hours)
DELETE FROM cte
WHERE RN > 1
Is it possible to remove first entered row of duplicate or i should have an extra column date_of_entry
? I want to do this if i entered same date_work and different hours PARTITION BY person_id, date_work
it remove randomly duplicates.
If it isn't possible, how can i remove duplicates with higher hours?
Add order by hours desc
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id, date_work
ORDER BY hours DESC) RN
FROM work_hours)
DELETE FROM cte
WHERE RN > 1
Yes - you or have to introduce date_of_entry field or some other vector field like IDENTITY
. For example if column Id
is your INT IDENTITY
, then your query will look like this:
;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY person_id, date_work, hours
ORDER BY ( SELECT Id DESC)) RN
FROM work_hours)
DELETE FROM cte
WHERE RN > 1
Of course it is valid if nobody changes the values in IDENTITY
column
And if your conditions suits - then you may want to use column Hours
as your vector field within grouping range of person_id, date_work
And even better way is to have an UNIQUE INDEX
over columns person_id, date_work, hours
, then there will no any ability to add duplicates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With