SQL 은 중복 데 이 터 를 조회 하고 중복 횟수 를 표시 합 니 다.

5180 단어 데이터 뱅 크
          
select * from F group by a,b,c,d having count(*)>1
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp

SQL 중복 데이터 삭제 방법
예 를 들 면:  id           name         value  1               a                 pp  2               a                 pp  3               b                 iii  4               b                 pp  5               b                 pp  6               c                 pp  7               c                 pp  8               c                 iii  id 는 메 인 키  이런 결 과 를 요구 하 다  id           name         value  1               a                 pp  3               b                 iii  4               b                 pp  6               c                 pp  8               c                 iii
방법 1 delete   YourTable    where   [id]   not   in   (  select   max([id])   from   YourTable    group   by   (name   +   value))  방법 2 delete   a  from   시계.   a   left   join(  select   id=min(id)   from   시계.   group   by   name,value  )b   on   a.id=b.id  where   b.id   is   null  중복 기록 을 조회 및 삭제 하 는 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) 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 main 필드 = @ id fetch cur rows into @ id, @ max end close cur rows set rowcount 0
방법 2 '중복 기록' 은 두 가지 의미 의 중복 기록 이 있 습 니 다. 하 나 는 완전히 중복 되 는 기록, 즉 모든 필드 가 중복 되 는 기록 입 니 다. 다른 하 나 는 일부 관건 적 인 필드 가 중복 되 는 기록 입 니 다. 예 를 들 어 Name 필드 가 중복 되 거나 다른 필드 가 중복 되 지 않 아 도 무시 할 수 있 습 니 다. 1. 첫 번 째 중복 에 대해 쉽게 해결 할 수 있 습 니 다. select distinct * from table N 을 사용 합 니 다.ame 는 중복 기록 이 없 는 결과 집합 을 얻 을 수 있 습 니 다. 이 표 에서 중복 기록 을 삭제 해 야 한다 면 (중복 기록 은 1 개 유지)다음 방법 으로 select distinct * into \ # Tmp from table Name drop table Name select * into table Name from \ # Tmp drop table \ # Tmp 에서 이러한 중복 이 발생 하 는 이 유 는 표 디자인 이 주도면밀 하지 않 아 유일한 색인 열 을 추가 하면 해결 할 수 있 기 때 문 입 니 다. 2. 이러한 중복 문 제 는 보통 중복 기록 의 첫 번 째 기록 을 유지 해 야 합 니 다. 조작 방법 은 다음 과 같 습 니 다.중 복 된 필드 가 Name, Address 라 고 가정 하면 이 두 필드 의 유일한 결과 집합 select idenity (int, 1, 1) as autoID, * into \ # Tmp from table Name selectmin (autoID) as autoID into \ # Tmp 2 from \ # Tmp group by Name, autoID select * from \ # Tmp where autoID in (select autoID from \ # tmp 2)마지막 select 는 Name, Address 가 중복 되 지 않 는 결과 집합 을 얻 었 습 니 다. (그러나 autoID 필드 가 하나 더 생 겼 습 니 다. 실제 쓸 때 select 자구 에 이 열 을 생략 할 수 있 습 니 다.) (4) 중복 select * from tablename where id in (selection id from tablename group by id having count (id) > 1)

좋은 웹페이지 즐겨찾기