edrp.cn的Blog

学习,需要交流,欢迎大家和我共同来学习C#,ASP.NET,MS SQL Server开发Web项目,欢迎大家和我交流

博客园 首页 新随笔 联系 订阅 管理

--查询重复的记录
SELECT a.* FROM Contact a,(
SELECT Cust_ID,Contact_Name
FROM Contact
GROUP BY Cust_ID,Contact_Name
HAVING COUNT(1)>1

) AS b
WHERE a.Cust_ID=b.Cust_ID AND a.Contact_Name=b.Contact_Name

---查询重复最小ID的记录
select MIN(aa.Contact_ID) id,Contact_Name from (
SELECT a.* FROM Contact a,(
SELECT Cust_ID,Contact_Name
FROM Contact
GROUP BY Cust_ID,Contact_Name
HAVING COUNT(1)>1

) AS b
WHERE a.Cust_ID=b.Cust_ID AND a.Contact_Name=b.Contact_Name
) aa
group by aa.Contact_Name
having COUNT(1)>1


--删除重复的记录
delete cc from Contact cc join(
select MIN(aa.Contact_ID) id,Contact_Name from (
SELECT a.* FROM Contact a,(
SELECT Cust_ID,Contact_Name
FROM Contact
GROUP BY Cust_ID,Contact_Name
HAVING COUNT(1)>1

) AS b
WHERE a.Cust_ID=b.Cust_ID AND a.Contact_Name=b.Contact_Name
) aa
group by aa.Contact_Name
having COUNT(1)>1
) tmp on cc.Contact_ID <>tmp.id and cc.Contact_Name =tmp.Contact_Name

 

posted on 2022-12-01 14:41  edrp.cn  阅读(237)  评论(0编辑  收藏  举报