17

我写了一个查询来从表中删除重复记录

;WITH a as
(
SELECT Firstname,ROW_NUMBER() OVER(PARTITION by Firstname, empID ORDER BY Firstname) 
AS duplicateRecCount
FROM dbo.tblEmployee
)
--Now Delete Duplicate Records
DELETE FROM tblEmployee
WHERE duplicateRecCount > 1 

但我不知道我哪里出错了

列名无效duplicateRecCount

有人能帮我吗?

4

2 回答 2

41

您需要在删除语句中引用 CTE...

WITH a as
(
SELECT Firstname,ROW_NUMBER() OVER(PARTITION by Firstname, empID ORDER BY Firstname) 
AS duplicateRecCount
FROM dbo.tblEmployee
)
--Now Delete Duplicate Records
DELETE FROM a
WHERE duplicateRecCount > 1
于 2013-02-24T16:48:34.897 回答
9
 DELETE duplicates FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY firstname, lastname, EMPNO, salary,dept  ORDER BY empno) cnt
 FROM tblEmp)  duplicates
WHERE duplicates.Cnt > 1
于 2013-04-14T10:28:46.620 回答