how to update (N-1) records from the N duplicate records in a database table (SQL SERVER 2005)
Background: I a开发者_JAVA技巧m generating a temporary table after comparing and inserting the records from the other two tables.
so the temp table have some records which have some duplicate fields (say: Order Id, Transaction Id etc...) - but are distinct
I have fetch such so called duplicate records but don't get an idea of how to update N-1 records among these N records.
Any help is appreciated (esp. sample code). Thanks in advance.
WITH duplicates AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY x,y,z ORDER BY a,b,c) AS duplicate_id,
*
FROM
myData
)
UPDATE
duplicates
SET
foo = bar
WHERE
duplicate_id > 1
x,y,z
are the fields needed to identify the duplicates. This is potentially all of the fields, depending on your definition of duplicate.
精彩评论