更新 sql server 数据库表中的 2 个重复项之一 [英] Update one of 2 duplicates in an sql server database table
问题描述
我有一个表,其中有一列包含重复值.我想更新 2 个重复值之一,例如 row1 = tom
和 row2 = tom
.. 我想向其中一个添加 1 或 a这将用于同一列中的许多其他重复项.基本上只需为每 1 个重复项添加一个数字或字母,这样就不会再有重复项了.
I have got a table that got a column with duplicate values. I would like to update one of a 2 duplicate values so for example row1 = tom
and row2 = tom
.. I want to add a 1 or an a to one of them and that will be for many other duplicates in the same column. Basically just add one number or letter to every 1 of the duplicates so there's no more duplicates.
我得到了这个查询,它将更新所有重复项,但不更新其中之一.有人可以帮忙吗?
I got this query that will update all the duplicates but not one of them. Can anyone help?
UPDATE Table1
SET Column1 = 'a'
WHERE exists
(SELECT Column1 , COUNT(Column1 )
FROM Clients
GROUP BY Column1
HAVING ( COUNT(Column1 ) > 1)
)
推荐答案
Try This with CTE
和 PARTITION BY
Try This with CTE
and PARTITION BY
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY Column1 ORDER BY Column1 ) AS rno,
Column1
FROM Clients
)
UPDATE cte SET Column1 =Column1 +' 1 '
WHERE rno=2
这篇关于更新 sql server 数据库表中的 2 个重复项之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!