我的数据库中有这个表:
tblAgencies
----------------------
AgencyID (PK)
VendorID
RegionID
Name
Zip
长话短说,我不小心将我的整个表复制到自身 - 所以我表中的每一行都有重复。
但是我的AgencyID
字段是标识,并且自动递增,我需要根据所有其他字段查找重复项,因为AgencyID
是唯一的。
有谁知道我该怎么做?
答案 0 :(得分:3)
这将保留最旧的AgencyID值,否则删除任何重复项。
;WITH x AS
(
SELECT *, rn = ROW_NUMBER() OVER
(PARTITION BY VendorID, RegionID, Name, Zip
ORDER BY AgencyID) FROM dbo.tblAgencies
)
DELETE x WHERE rn > 1;
但是,要小心;如果其他表引用了AgencyID 和,他们已经获得了任何较新的,错误的值,这可能无效。
答案 1 :(得分:1)
最简单的解决方案,将select distinct用于临时表,然后重新加载原始
答案 2 :(得分:0)
如果所有其他列的组合都是唯一的,则此查询将为您提供重复项:
select * from mytable t1
where exists
(select * from mytable t2
where t1.VendorID = t2.VendorID
and t1.RegionID = t2.RegionID
and and t1.Name = t2.Name
and t1.Zip = t2.Zip
and t1.AgencyID > t2.AgencyID)
答案 3 :(得分:0)
除了最小的agencyid行外,这应该为您提供具有重复值的所有行。
select *
from tblAgencies
where AgencyID not in (select min(AgencyID)
from tblAgencies
group by VendorID, RegionID, Name, Zip)
编辑:添加SQLFiddle
答案 4 :(得分:0)
;with CTE
AS
(
SELECT ID_Column, rn = ROW_NUMBER() OVER (PARTITION BY Column1, Column2, Column3... ORDER BY ID ASC)
FROM T
)
DELETE FROM CTE
WHERE rn >= 2
答案 5 :(得分:0)
;with CTE
AS
(SELECT MAX(AgencyID) AgentID,VendorID ,
RegionID ,
Name ,
Zip FROM tblAgencies
GROUP BY VendorID ,
RegionID ,
Name ,
Zip
HAVING COUNT(*) > 1)
DELETE FROM tblAgencies WHERE EXISTS (SELECT 1 FROM CTE
WHERE AgentID = tblAgencies.AgencyID)
答案 6 :(得分:0)
很多答案会在这里给你你想要的东西,但是没有必要使用CTE或进行任何分组,最简单的方法就是:
delete t1
from tblAgencies t1
join tblAgencies t2
on t1.VendorId = t2.VendorId
and t1.RegionId = t2.RegionId
and t1.Name = t2.Name
and t1.Zip = t2.Zip
and t1.AgencyId > t2.AgencyId
答案 7 :(得分:0)