我需要从我的员工表中删除重复项,但是如果重复项以相同的公司名称出现,则应将其保留。
我有以下数据
empid resource Dpart company
111 alex AA 1C
112 chris QA 2F
111 alex AA 1C
111 alex AA 2F
111 alex AA 2F
111 alex AA 5F
我尝试了row_number()over(partition),但是没有给出我期望的结果。请帮助我们。
delete duplicates from (SELECT empid,
resouces,department ,ROW_NUMBER() OVER (PARTITION BY empid,
resouces,departmentORDER BY empid) cnt FROM
(select empid, resouces,department from Employee )t1)
duplicates WHERE duplicates.Cnt > 1
根据以上数据,重复删除员工表后
empid resource Dpart company
111 alex AA 1C
112 chris QA 2F
111 alex AA 1C
答案 0 :(得分:1)
我认为您需要DENSE_RANK()
代替:
DELETE e
FROM (SELECT e.*,
DENSE_RANK() OVER (PARTITION BY empid, resource, Dpart ORDER BY company) AS Seq
FROM Employee e
) e
WHERE e.Seq > 1;
答案 1 :(得分:0)
使用dense_rank
with cte as
(
select *,dense_rank() over(partition by empid order by company) as rn
from Employee
)delete from cte where rn>1
答案 2 :(得分:0)
下面我举一个例子:
-创建表格
CREATE TABLE dbo.DiaChiEmail(
ID INT IDENTITY PRIMARY KEY,
Email VARCHAR(100),
Ten NVARCHAR(100)
)
GO
-插入一些记录
INSERT dbo.DiaChiEmail(Email,Ten)
SELECT 'abc@gmail.com', 'abc' UNION ALL -- #1
SELECT 'def@yahoo.com', 'def' UNION ALL -- #2
SELECT 'ghi@hotmail.com', 'ghi' UNION ALL -- #3
SELECT 'jkl@ymail.com', 'jkl' UNION ALL -- #4
SELECT 'abc@gmail.com', 'abc 2' UNION ALL -- #5, dupl with #1
SELECT 'abc@gmail.com', 'abc 3' UNION ALL -- #6, dupl with #1
SELECT 'ghi@hotmail.com', 'ghi 2' -- #7, dupl with ghi #3
要删除重复记录的查询是:
WITH [TBL_DUPLICATE] AS
(
SELECT
STT = ROW_NUMBER() OVER (PARTITION BY Email ORDER BY ID DESC), Email
FROM DiaChiEmail
)
DELETE FROM [TBL_DUPLICATE] WHERE STT > 1