0

我有一个包含大约 90,000 个条目的数据库表。我需要更新任何较旧的条目,以便将它们移动到存档表中。我能够找到计数大于 1 的重复项,并找出它被重复了多少次。

这是我用来工作的查询。

 SELECT DWPAGECOUNT, DOCTYPE, FILENAME, First, middleinitial, last, 
     COUNT(*) as Number_of_Duplicates
   FROM dbo.REGISTRAR
  WHERE first IS NOT NULL
  GROUP BY DWPAGECOUNT, DOCTYPE, FILENAME, First, middleinitial, last
 HAVING COUNT(*) > 1
  ORDER by Number_of_Duplicates desc 

我现在需要多次更新表中的任何内容,并保持最新条目不变,并将状态列更新为重复。

我怎样才能做到这一点?

在此先感谢您的帮助。

4

1 回答 1

1
;WITH x AS 
(
  SELECT DWPAGECOUNT, DOCTYPE, FILENAME, First, middleinitial, last, 
    [STATUS],
    rn = ROW_NUMBER() OVER 
         (
           PARTITION BY 
             DWPAGECOUNT, DOCTYPE, FILENAME, First, middleinitial, last
           ORDER BY 
             STOREDATETIME DESC
         )
  FROM dbo.REGISTRAR
)
UPDATE x SET [STATUS] = CASE WHEN rn > 1 THEN 'DUP' ELSE 'NOT DUP' END;
于 2013-04-23T16:37:55.230 回答