日韩精品中文字幕一区二区-日韩精品中文字幕一区-日韩精品中文字幕视频-日韩精品在线一区二区三区-第一页在线-第一福利视频

當(dāng)前位置: 首頁IT技術(shù) → sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法

sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法

更多

重復(fù)數(shù)據(jù),通常有兩種:一是完全重復(fù)的記錄,也就是所有字段的值都一樣;二是部分字段值重復(fù)的記錄。

一. 刪除完全重復(fù)的記錄

完全重復(fù)的數(shù)據(jù),通常是由于沒有設(shè)置主鍵/唯一鍵約束導(dǎo)致的。

測試數(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) 借助臨時表

利用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

如果多個表有完全重復(fù)的行,可以考慮通過UNION將多個表聯(lián)合,插到一個新的同結(jié)構(gòu)的表,SQL Server會幫助去掉表和表之間的重復(fù)行。

二. 刪除部分重復(fù)的記錄

部分列重復(fù)的數(shù)據(jù),通常表上是有主鍵的,可能是程序邏輯造成了多行數(shù)據(jù)列值的重復(fù)。

測試數(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) 唯一索引

唯一索引有個忽略重復(fù)建的選項,在創(chuà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) 借助主鍵/唯一鍵來刪除

通常會選擇主鍵/唯一鍵的最大/最小值保留,其他行刪除。以下只保留重復(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

 

熱門評論
最新評論
發(fā)表評論 查看所有評論(1)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
主站蜘蛛池模板: 游吟诗人| 误杀2 电影| 难兄难弟 电影| 成人在线播放网站| 十大黄色软件推荐免费| 午夜高清影院| 陈慧娴个人资料| 游泳池play高h| 混沌行走| 同城招聘网找工作| 秀人网 官网门户免费| 内蒙古电视台雷蒙| 你们可知道正谱| 热点视频| 朱砂手串的功效与作用佩戴禁忌| 秀人网xiuren大尺度| 江湖之社团风暴| city of stars歌词| 贝利亚抱住奥特之母完整版| 抚养费标准一般是多少钱一个月| 肢体的诱惑电影| 奇门遁甲电影免费观看完整版| 退社申请书800字| 3片| 郭亚菲| cctv16节目表今天目表| 董三毛| 口加一笔变新字有几个| 我想成为影之强者| 吻胸亲乳激情大尺度| 黄视频免费看网站| 赵琦| 重温经典节目预告| 零下100度| 贪玩的小水滴想象作文400字左右| 一问倾城| 天下第一楼演员表全部| 混的头像| 江苏体育频道直播| 杨玉环秘史| 石璐|