oracle查詢重複資料和刪除重覆記錄示例分享

2022-09-24 14:24:11 字數 4270 閱讀 8215

一、查詢某個字段重複

select *

from user u

where u.user_name in (select u.user_name

from user u

group by u.user_name h**ing count(*) > 1)

二,刪除表中某幾個欄位的重複

例:表中有條六條記錄。   其中張三和王五   的記錄有重複

tablea

id customer phoneno

001 張三 777777

002 李四 444444

003 王五 555555

004 張三 777777

005 張三 777777

006 王五 555555

如何寫乙個sql語句將tablea變成如下

001 張三 777777

002 李四 444444

003 王五 555555

測試環境

create table tablea ( id varchar(3),customer varchar(5),phoneno varchar(6))

insert into tablea select '001','張三','777777'

union all select '002ymwrd','李四','444444'

u程式設計客棧nion all select '003','王五','555555'

union all select '004','張三','777777'

union all select '005','張三','777777'

union all select '006','王五','555555'

結果delete tablea from tablea twhere

exists(

select 1fromtablea where customer=t.customer and phoneno=t.phoneno

andid < tt.id)總結

該方法適用於有乙個欄位為自增性,例如本例中的:id

delete 表名 from 表名 as twhere

exists(

select 1from表名 where 欄位a=t.欄位a and 欄位b=t.欄位b,(....)

and自增列 < t.自增列

)三,查詢並刪除重覆記錄的sql語句

查詢及刪除重覆記錄的sql語句

1、查詢表中多餘的重覆記錄,重覆記錄是根據單個字段(peopleid)來判斷

select * from people

where peopleid in (select peopleid from people group by peopleid h**ing count(peopleid) > 1)

2、刪除表中多餘的重覆記錄,重覆記錄是根據單個字段(peopleid)來判斷,只留有rowid最小的記錄

delete from people

where peopleid in (select peopleid from people group by peopleid h**ing count(peopleid) > 1)

and rowid not in (select min(rowid) from people group by peopleid h**ing count(peopleid )>1)

注:rowid為oracle自帶不用該.....

3、查詢表中多餘的重覆記錄(多個字段)

select * from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq h**ing count(*) > 1)

4、刪除表中多餘的重覆記錄(多個字段),只留有rowid最小的記錄

delete from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq h**ing count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleid,seq h**ing count(*)>1)

5、查詢表中多餘的重覆記錄(多個字段),不包含rowid最小的記錄

select * from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq h**ing count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleid,seq h**ing count(*)>1)

ymwrd(二)

比方說

在a表中存在乙個字段「name」,

而且不同記錄之間的「name」值有可能會相同,

現在就是需要查詢出在該表中的各記錄之間,「name」值存在重複的項;

select name,count(*) from a group by name h**ing count(*) > 1

如果還查性別也相同大則如下:

select name,***,count(*) from a group by name,*** h**ing count(*) > 1

(三)方法一

declare @max integer,@id integer

declare cur_rows cursor local for select 主欄位,count(*) from 表名 group by 主欄位 h**ing count(*) >; 1

open cur_rows

fetch cur_rows into @id,@max

while @@fetch_status=0

begin

select @max = @max -1

set rowcount @max

delete from 表名 where 主欄位 = @id

fetch cur_rows into @id,@max

end

close cur_rows

set rowcount 0 方法二

"重覆記錄"有兩個意義上的重覆記錄,一是完全重複的記錄,也即所有欄位均重複的記錄,二是部分關鍵字段重複的記錄,比如name欄位重複,而其他欄位不一定重複或都重複可以忽略。

1、對於第一種重複,比較容易解決,使用

select distinct * from tablename

就可以得到無重覆記錄的結果集。

如果該錶需要刪除重複的記錄(重覆記錄保留1條),可以按以下方法刪除

select distinct * into #tmp from tablename

drop table tablename

select * into tablename from #tmp

drop table #tmp

發生這種重複的原因是表設計不周產生的,增加唯一索引列即可解決。

2、這類重複問題通常要求保留重覆記錄中的第一條記錄,操作方法如下

假設有重複的字段為name,address,要求得到這兩個字段唯一的結果集

select identity(int,www.cppcns.com1,1) as autoid, * into #tmp from tablename

select min(autoid) as autoid into #tmp2 from #tmp group by name,autoid

select * from #tmp where autoid in(select autoid from #tmp2)

最後乙個select即得到了name,address不重複的結果集(但多了乙個autoid欄位,實際寫時可以寫在select子句中省去此列)

(四)

查詢重複

select * from tablename where id in (

select id from tablename

group by id

h**ing count(id) > 1

)本文標題: oracle查詢重複資料和刪除重覆記錄示例分享

本文位址: /shujuku/oracle/103379.html

Oracle刪除重複資料

在oracle中,有個隱藏了自動rowid,裡面給每條記錄乙個唯一的rowid,我們如果想保留最新的一條記錄,我們就可以利用這個字段,保留重複資料中rowid最大的一條記錄就可以了。下面是查詢重複資料的乙個例子 select a.rowid,a.from 表名 a where a.rowid sel...

ORACLE刪除重複資料

我們可能會出現這種情況,某個表原來設計不周全,導致表裡面的資料資料重複,那麼,如何對重複的資料進行刪除呢?重複的資料可能有這樣兩種情況,第一種時表中只有某些字段一樣,第二種是兩行記錄完全一樣。一 對於部分字段重複資料的刪除 先來談談如何查詢重複的資料吧。下面語句可以查詢出那些資料是重複的 selec...

oracle 複製資料 刪除重複資料

最簡單的複製 insert into a kcdm,zklb,kksj i select kcdm,zklb,kksj i from b 產生疑惑的地方 如果a有乙個自增的id,語句該怎麼寫?開始想法 insert into a id,kcdm,zklb,kksj i select seq a id...