更新sql server数据库表中的2个重复项之一

时间:2013-07-22 09:23:01

标签: sql sql-server

我有一个表格,其中包含重复值的列。我想更新2个重复值中的一个,例如row1 = tomrow2 = tom ..我想在其中一个中添加1或a,这将是其中的许多其他重复项同一栏。基本上只需要为每1个重复项添加一个数字或字母,这样就不会有重复数据。

我得到了这个查询,它将更新所有重复项,但不会更新其中一个。有人可以帮忙吗?

UPDATE Table1
   SET Column1 = 'a'
 WHERE exists
       (SELECT Column1 , COUNT(Column1 )
FROM Clients
GROUP BY Column1 
HAVING ( COUNT(Column1 ) > 1)
)

13 个答案:

答案 0 :(得分:38)

使用CTEPARTITION 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

答案 1 :(得分:5)

我认为这个简单的更新是您正在寻找的;

UPDATE Table1 SET Column1=Column1+CAST(id AS VARCHAR)
WHERE id NOT IN (
  SELECT MIN(id) 
  FROM Table1
  GROUP BY Column1
);

输入:

(1,'A'),
(2,'B'),
(3,'A'),
(4,'C'),
(5,'C'),
(6,'A');

输出:

(1,'A'),
(2,'B'),
(3,'A3'),
(4,'C'),
(5,'C5'),
(6,'A6');

An SQLfiddle to test with

答案 2 :(得分:4)

我认为您可以使用TOP()运算符代替row_number()方法,它将帮助您以简单方便的方式更新一个

UPDATE TOP ( 1 )Table1 SET Column1 = 'a';

答案 3 :(得分:3)

假设Table1,包含以下信息:

Column1      
========  
tom        
john   
jack    
tom     
james
jane 

请注意,第一行和第四行是相同的。这是UPDATE命令,只在其中一个中更改名称。

UPDATE Table1 AS t1
   SET Column1 = 'jennifer'
 WHERE rrn(t1) =
       (SELECT MAX(rrn(t2))
          FROM Table1 AS t2
         WHERE Column1 = 'tom')

结果将是

Column1      
========  
tom        
john   
jack    
jennifer
james
jane 

使用RRN功能,您可以更新重复记录的最后一次出现

答案 4 :(得分:1)

如果我们有这种情况:

SELECT Column1 FROM Clients ORDER BY Column1

Column1
**********
Jhon
Jhon
Mike
Mike
Mike
Robert

即使Column1被重复了n次,此操作仍然有效,它将行号附加到除第一行之外的所有重复行中,请尝试以下操作:

BEGIN 

;WITH CTE AS 
(
    SELECT 
        ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY Column1) AS ROWNUMBER,
        Column1
    FROM Clients
)

UPDATE CTE 
SET Column1 = CONCAT(Column1, ' ', (ROWNUMBER - 1))
WHERE ROWNUMBER > 1

SELECT Column1 FROM Clients ORDER BY Column1

END 

结果:

Column1
***********
Jhon
Jhon 1
Mike
Mike 1
Mike 2
Robert

答案 5 :(得分:0)

试试这个伴侣

UPDATE Table1 
SET Column1 = column1 +'a'   
WHERE exists(
        select row 
        from (
            SELECT 
                Column1 , 
                Row_Number() over(Partition by Column1 order by Column1) as row 
            FROM Clients
        ) as subquery 
        where subquery.row = 2
    )

答案 6 :(得分:0)

由于我的sql开发人员不喜欢更新with,因此我不得不把内置于一个 复制来自Nithesh的答案

它最终看起来像这样:

UPDATE Clients 
SET Column1 = 'a'
WHERE ColumnID IN (select ColumnID from 
 (  
 SELECT
       row_number() OVER(PARTITION BY Column1  ORDER BY Column1 ) AS rno,
       ColumnID FROM Clients
 )
                   where rno=2   
                  )

答案 7 :(得分:0)

试试这个

with test as 
(
   select ROW_NUMBER() over (order by salary)rr, salary , emp_no 
   from salary
)update test set emp_no=10007 where emp_no='10002'  and rr=3

答案 8 :(得分:0)

UPDATE table1
SET STATUS = 'F'
WHERE column1= 
   (SELECT (column1)
    FROM table1
    GROUP BY column1 
    HAVING (COUNT(column1 ) > 1))

答案 9 :(得分:0)

update wmwhse1.sku
set descr= concat (descr,'.')
where exists
(select SKU,count (DESCR)
from wmwhse1.sku
group by descr
having (count (DESCR)>1))

当我运行此脚本时,所有sku描述都在更新。

输出:仅应影响1行

答案 10 :(得分:0)

我找到了此解决方案:

我的表设备中已经有数据,并且“序列”列应该是唯一的。 “ id”是主键。原始值后会连接一个随机的6位数字值。

UPDATE devices 
SET serial=CONCAT(serial,'_',LPAD(FLOOR(RAND() * 999999.99), 6, '0'))    
    where id in 
    (select * FROM( 
        SELECT d1.id as id 
        FROM devices as d1, devices as d2 
        WHERE d1.id <> d2.id and d1.serial=d2.serial) as subdevices
    )

答案 11 :(得分:0)

免责声明:

  1. 查询语言:Microsoft SQL 查询
  2. 步骤在以下注释中得到澄清:--

问题:使用“D”更新最后一个 ID,其中年、月是重复的成员。

-- Sample Table Creation

DECLARE @TempTable TABLE
( Id INT IDENTITY (1, 1) NOT NULL, MemberId INT, for_year INT, for_month INT, for_status CHAR(1) )

-- Sample Insert
INSERT INTO @TempTable
SELECT '2', '2077', '11', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '11', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '77', '2076', '10', 'A' UNION ALL
SELECT '77', '2076', '11', 'A' UNION ALL
SELECT '77', '2076', '12', 'A'

-- where expectation meets reality :) -- logic / main query
UPDATE @TempTable SET for_status = 'D' WHERE Id IN 
(
SELECT MAX(Id) FROM @TempTable GROUP BY MemberId, for_year, for_month HAVING COUNT(*)>1
)

-- Sample Select
SELECT * FROM @TempTable

答案 12 :(得分:0)

我找到了一个解决方案:

在表格中插入一个虚拟列

更新列中的值并给出随机值

使用该虚拟列的值更新该行并删除该列。

Step 1 :
Alter table table_name
ADD column dummy_column

Step 2:
Update table_name
SET dummy_column = (RAND() * 10) 
where duplicate_column = duplicate_value

Step 3:
Update table_name
SET duplicate_column = new_value
where dummy_column = random_value

Step 4:
Alter table table_name
drop column dummy_column