0

我有下表,并且我想将已删除列的值设置为零,仅针对具有重复名称的记录之一。

id     name      deleted
------------------------
1      a         1
2      a         1
3      a         1
4      b         1
5      c         1
6      d         1

所以输出将是:

id     name      deleted
------------------------
1      a         0
2      a         1
3      a         1
4      b         0
5      c         0
6      d         0
4

3 回答 3

3

如果您的 dbms 是 SQL-Server(>= 2005),您可以使用CTEwith ROW_NUMBER

WITH CTE AS
(
   SELECT ID, Name, Deleted,
     RN = ROW_NUMBER() OVER(PARTITION BY name ORDER BY ID)
   FROM dbo.T
)
UPDATE CTE 
SET Deleted = 0
WHERE RN = 1

演示

于 2013-01-16T16:47:46.110 回答
1
UPDATE Tbl SET deleted = 0 WHERE id IN
(SELECT MIN(id) FROM Tbl GROUP BY name)
于 2013-01-16T16:49:03.233 回答
0
UPDATE A
SET A.deleted = 0
FROM Tbl A
   LEFT JOIN Tbl B
     ON A.Name = B.Name
        AND A.Id > B.Id
WHERE B.Id IS NULL
于 2013-01-16T16:43:14.007 回答