SQL Server查找與重復記錄的刪除方案描述
此文章主要向大家介紹的是SQL Server查找與刪除重復記錄的實際操作方法,你是否對如何正確查出字段dd中有重復的記錄的實際操作有不解之處?即要知道哪些記錄是重復的,怎么弄?
執(zhí)行:
- select dd,count(*) from table group by dd having count(*)>1
如何用sql 查找兩個字段重復的記錄,并列出重復記錄
表名為CJB, 列出其中XH和KCMC字段都重復的記錄
執(zhí)行:
- select * from CJB a join (
- select XH,KCMC from CJB group by XH,KCMC
- having count(*)>1) b on a.XH=b.XH and a.KCMC=b.KCMC order by
- a.KCMC ,a.XH
實例:
- select productID,searchkay from shortkay group by searchkay,productID having count(*) > 1 order by searchkay
等同于下面這句:
- select a.ID,a.productID,a.searchkay from shortkay a join (select productID,searchkay from shortkay group by productID,
- searchkay having count(*)>1) b on a.productID=b.productID and a.searchkay=b.searchkay order by a.searchkay, a.productID
1、SQL Server查找表中多余的重復記錄,重復記錄是根據(jù)單個字段(peopleId)來判斷
- select * from people
- where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、刪除表中多余的重復記錄,重復記錄是根據(jù)單個字段(peopleId)來判斷,只留有rowid最小的記錄
- delete from people
- where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
- and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1
)
3、查找表中多余的重復記錄(多個字段)
- select * from vitae a
- where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、刪除表中多余的重復記錄(多個字段),只留有rowid最小的記錄
- delete from vitae a
- where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
- and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、SQL Server查找表中多余的重復記錄(多個字段),不包含rowid最小的記錄
- select * from vitae a
- where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
- and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
以上的相關內(nèi)容就是對SQL Server查找和刪除重復記錄的方法的介紹,望你能有所收獲。
【編輯推薦】