sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法
一. 刪除完全重復(fù)的記錄
完全重復(fù)的數(shù)據(jù),通常是由于沒有設(shè)置主鍵/唯一鍵約束導(dǎo)致的。
測(cè)試數(shù)據(jù):
if OBJECT_ID('duplicate_all') is not null
drop table duplicate_all
GO
create table duplicate_all
(
c1 int,
c2 int,
c3 varchar(100)
)
GO
insert into duplicate_all
select 1,100,'aaa' union all
select 1,100,'aaa' union all
select 1,100,'aaa' union all
select 1,100,'aaa' union all
select 1,100,'aaa' union all
select 2,200,'bbb' union all
select 3,300,'ccc' union all
select 4,400,'ddd' union all
select 5,500,'eee'
GO
(1) 借助臨時(shí)表
利用DISTINCT得到單條記錄,刪除源數(shù)據(jù),然后導(dǎo)回不重復(fù)記錄。
如果表不大的話,可以把所有記錄導(dǎo)出一次,然后truncate表后再導(dǎo)回,這樣可以避免delete的日志操作。
if OBJECT_ID('tempdb..#tmp') is not null
drop table #tmp
GO
select distinct * into #tmp
from duplicate_all
where c1 = 1
GO
delete duplicate_all where c1 = 1
GO
insert into duplicate_all
select * from #tmp
(2) 使用ROW_NUMBER
with tmp
as
(
select *,ROW_NUMBER() OVER(PARTITION BY c1,c2,c3 ORDER BY(getdate())) as num
from duplicate_all
where c1 = 1
)
delete tmp where num > 1
如果多個(gè)表有完全重復(fù)的行,可以考慮通過UNION將多個(gè)表聯(lián)合,插到一個(gè)新的同結(jié)構(gòu)的表,SQL Server會(huì)幫助去掉表和表之間的重復(fù)行。
二. 刪除部分重復(fù)的記錄
部分列重復(fù)的數(shù)據(jù),通常表上是有主鍵的,可能是程序邏輯造成了多行數(shù)據(jù)列值的重復(fù)。
測(cè)試數(shù)據(jù):
if OBJECT_ID('duplicate_col') is not null
drop table duplicate_col
GO
create table duplicate_col
(
c1 int primary key,
c2 int,
c3 varchar(100)
)
GO
insert into duplicate_col
select 1,100,'aaa' union all
select 2,100,'aaa' union all
select 3,100,'aaa' union all
select 4,100,'aaa' union all
select 5,500,'eee'
GO
(1) 唯一索引
唯一索引有個(gè)忽略重復(fù)建的選項(xiàng),在創(chuàng)建主鍵約束/唯一鍵約束時(shí)都可以使用這個(gè)索引選項(xiàng)。
if OBJECT_ID('tmp') is not null
drop table tmp
GO
create table tmp
(
c1 int,
c2 int,
c3 varchar(100),
constraint UQ_01 unique(c2,c3) with(IGNORE_DUP_KEY = ON)
)
GO
insert into tmp
select * from duplicate_col
select * from tmp
(2) 借助主鍵/唯一鍵來刪除
通常會(huì)選擇主鍵/唯一鍵的最大/最小值保留,其他行刪除。以下只保留重復(fù)記錄中c1最小的行。
delete from duplicate_col
where exists(select 1 from duplicate_col b where duplicate_col.c1 > b.c1 and (duplicate_col.c2 = b.c2 and duplicate_col.c3 = b.c3))
--或者
delete from duplicate_col
where c1 not in (select min(c1) from duplicate_col group by c2,c3)
如果要保留重復(fù)記錄中的第N行,可以參考05.取分組中的某幾行。
(3) ROW_NUMBER
和刪除完全重復(fù)記錄的寫法基本一樣。
with tmp
as
(
select *,ROW_NUMBER() OVER(PARTITION BY c2,c3 ORDER BY(getdate())) as num
from duplicate_col
)
delete tmp where num > 1
select * from duplicate_col
SQL刪除重復(fù)數(shù)據(jù)只保留一條 (下面的代碼,很多網(wǎng)友反饋錯(cuò)誤,大家多測(cè)試)
用SQL語句,刪除掉重復(fù)項(xiàng)只保留一條
在幾千條記錄里,存在著些相同的記錄,如何能用SQL語句,刪除掉重復(fù)的呢
1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷,只留有rowid最小的記錄
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)
3、查找表中多余的重復(fù)記錄(多個(gè)字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有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、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含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)
6.消除一個(gè)字段的左邊的第一位:
update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'
7.消除一個(gè)字段的右邊的第一位:
update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'
8.假刪除表中多余的重復(fù)記錄(多個(gè)字段),不包含rowid最小的記錄
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId
- MySQL數(shù)據(jù)庫中刪除重復(fù)記錄的方法總結(jié)[推薦]
- sqlserver中重復(fù)數(shù)據(jù)值只取一條的sql語句
- mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解
- MySQL根據(jù)某一個(gè)或者多個(gè)字段查找重復(fù)數(shù)據(jù)的sql語句
- mysql數(shù)據(jù)庫刪除重復(fù)數(shù)據(jù)只保留一條方法實(shí)例
- Mysql?刪除重復(fù)數(shù)據(jù)保留一條有效數(shù)據(jù)(最新推薦)
- oracle/mysql數(shù)據(jù)庫多條重復(fù)數(shù)據(jù)如何取最新的
相關(guān)文章
簡(jiǎn)單觸發(fā)器的使用 獻(xiàn)給SQL初學(xué)者
簡(jiǎn)單觸發(fā)器的使用 獻(xiàn)給SQL初學(xué)者,使用sqlserver的朋友可以參考下。2011-09-09MSSQL批量插入數(shù)據(jù)優(yōu)化詳細(xì)
這篇文章主要為大家分享一下批量插入數(shù)據(jù)的方法,有時(shí)候我們需要插入大量的數(shù)據(jù)那么就需要優(yōu)惠了,要不根本受不了2017-07-07SQL?Server中元數(shù)據(jù)函數(shù)的用法
這篇文章介紹了SQL?Server中元數(shù)據(jù)函數(shù)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05SQL Server 2012使用Offset/Fetch Next實(shí)現(xiàn)分頁數(shù)據(jù)查詢
在Sql Server 2012之前,實(shí)現(xiàn)分頁主要是使用ROW_NUMBER(),在SQL Server2012,可以使用Offset ...Rows Fetch Next ... Rows only的方式去實(shí)現(xiàn)分頁數(shù)據(jù)查詢,具體代碼詳解大家參考下本文2017-07-07Sql存儲(chǔ)過程游標(biāo)循環(huán)的用法及sql如何使用cursor寫一個(gè)簡(jiǎn)單的循環(huán)
這篇文章主要介紹了循環(huán)和游標(biāo)在Sql存儲(chǔ)過程中使用及sql如何使用cursor寫一個(gè)簡(jiǎn)單的循環(huán)的相關(guān)資料,需要的朋友可以參考下2015-11-11Windows故障轉(zhuǎn)移群集 和 SQLServer AlwaysOn 配置搭建詳
這篇文章主要介紹了Windows故障轉(zhuǎn)移群集 和 SQLServer AlwaysOn 搭建教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03SQL Server數(shù)據(jù)類型char、nchar、varchar、nvarchar的區(qū)別淺析
這篇文章主要介紹了SQL Server數(shù)據(jù)類型char、nchar、varchar、nvarchar的區(qū)別淺析,本文寫的比較容易明白,需要的朋友可以參考下2014-06-06Sql學(xué)習(xí)第一天——SQL 練習(xí)題(建表/sql語句)
來自Madrid且訂單數(shù)少于3的消費(fèi)者,針對(duì)這個(gè)要求作出以下:建表 做題分析以及sql語句的寫法,感興趣的朋友可以參考下哈,希望可以幫助到你2013-03-03