更新 sql server 数据库表中的 2 个重复项之一 [英] Update one of 2 duplicates in an sql server database table

查看:18
本文介绍了更新 sql server 数据库表中的 2 个重复项之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有一列包含重复值.我想更新 2 个重复值之一,例如 row1 = tomrow2 = 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 CTEPARTITION 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆