Oacle 중복 데이터 조회 및 중복 기록 삭제 예제 공유
5131 단어 Oacle 중복 데이터 조회
select *
from User u
where u.user_name in (select u.user_name
from User u
group by u.user_name having count(*) > 1)
2.표 의 몇 필드 의 중복 을 삭제 합 니 다.예:표 에 여섯 개의 기록 이 있다. 그 중 장삼 과 왕 오 반복
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 '002',' ','444444'
union 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.
)
3.중복 기 록 된 SQL 문 구 를 조회 하고 삭제 합 니 다.중복 기 록 된 SQL 문장 조회 및 삭제
1.표 에 남아 있 는 중복 기록 을 찾 습 니 다.중복 기록 은 단일 필드(peopleid)에 따라 판단 합 니 다.
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2.표 에 남아 있 는 중복 기록 을 삭제 하고 중복 기록 은 하나의 필드(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)
주:rowid 는 Oacle 을 위 한 자체 테이프 입 니 다.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.표 에 남아 있 는 중복 기록(여러 필드)을 찾 습 니 다.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)
(2)예 를 들 면
A 표 에 필드"name"이 존재 합 니 다.
그리고 서로 다른 기록 사이 의"name"값 이 같 을 수 있 습 니 다.
이 표 의 각 기록 사 이 를 조회 해 야 합 니 다."name"값 에 중복 되 는 항목 이 있 습 니 다.
Select Name,Count(*) From A Group By Name Having Count(*) > 1
성별 도 같다 면 다음 과 같다.
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
(3)방법 1
declare @max integer,@id integer
declare cur_rows cursor local for select ,count(*) from group by having 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 방법 2'중복 기록'은 두 가지 의미 의 중복 기록 이 있 습 니 다.하 나 는 완전히 중복 되 는 기록 입 니 다.즉,모든 필드 가 중복 되 는 기록 입 니 다.다른 하 나 는 일부 관건 적 인 필드 가 중복 되 는 기록 입 니 다.예 를 들 어 Name 필드 가 중복 되 거나 다른 필드 가 중복 되 지 않 아 도 무시 할 수 있 습 니 다.
1.첫 번 째 중복 에 대해 비교적 쉽게 해결 하고 사용
select distinct * from tableName
중복 기록 이 없 는 결과 집 을 얻 을 수 있 습 니 다.
이 표 가 중 복 된 기록 을 삭제 해 야 한다 면 다음 과 같은 방법 으로 삭제 할 수 있다.
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 자구 에 이 열 을 줄 일 수 있 습 니 다)(4)
조회 중복
select * from tablename where id in (
select id from tablename
group by id
having count(id) > 1
)