开发者

how to update (N-1) records from the N duplicate records in a database table (SQL SERVER 2005)

开发者 https://www.devze.com 2023-03-26 20:34 出处:网络
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 f

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消