ORACLE查詢刪除重覆記錄三種方法

2022-09-22 00:21:11 字數 4442 閱讀 6908

比如現在有一人員表 (表名:peosons)

若想將姓名、身份證號、住址這三個字段完全相同的記錄查詢出來

複製** **如下:

select p1.*  

from persons  p1,persons  p2  

where p1.id<>p2.id  

and  p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address

可以實現上述效果.

幾個刪除重覆記錄的sql語句

1.用rowid方法

2.用group by方法

3.用distinct方法

1。用rowid方法

據據oracle帶的rowid屬性,進行判斷,是否存在重複,語句如下:

查資料:

複製** **如下:

select * from table1 a where rowid !=(select max(rowid)

from table1 b where a.ekpxcyrname1=b.name1 and a.name2=b.name2......)

刪資料:

複製** **如下:

delete  from table1 a where rowid !=(select max(rowid)

from table1 b where a.name1=b.name1 and a.name2=b.name2......)

2.group by方法

查資料:

複製** **如下:

select count(num), max(name) from student --列出重複的記錄數,並列出他的name屬性

group by num

h**ing count(num) >1 --按num分組後找出表中num列重複,即出現次數大於一次

刪資料:

複製** **如下:

delete from student

group by num

h**ing count(num) >1

這樣的話就把所有重複的都刪除了。

3.用distinct方法 -對於小的表比較有用

複製** **如下:

create table table_new as   select distinct *   from table1 minux

truncate table table1;

insert into table1 select * from table_new;

查詢及刪除重覆記錄的方法大全

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)

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 (sel min(rowid) from vitae group by peopleid,seq h**ing count(*)>1)

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

複製** **如下:

select * from vekpxcyritae 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(*)&gwww.cppcns.comt;1)

(二)比方說在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

declarwww.cppcns.come 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

endclose 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,1,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查詢刪除重覆記錄三種方法

本文位址:

SQL查詢重覆記錄,刪除重覆記錄

1 查詢表中多餘的重覆記錄,重覆記錄是根據單個字段 docid 來判斷 select from tablename where docid in select docid from tablename group by docid h ing count docid 1 例二 select from...

Oracle刪除重覆記錄

oracle刪除重覆記錄的最好的方法 delete from emp e where e.rowid select min y.rowid from emp y where y.empno e.empno 1 子查詢找出某員工最低的rowid 肯定只有乙個 其它大於這條記錄rowid的,全部刪除。2...

快速刪除ORACLE重覆記錄

在oracle中,可以通過唯一rowid實現刪除重覆記錄 還可以建臨時表來實現.這個只提到其中的幾種簡單實用的方法,希望可以和大家分享 以表employee為例 sql desc employee name null?type emp id number 10 emp name varchar2 2...