database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
ㆍDB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
DB2 Q&A 1182 게시물 읽기
No. 1182
키 안잡힌 테이블에서 중복 데이터 조회 후 삭제하기
작성자
김중인(jagalchi)
작성일
2006-11-21 10:57ⓒ
2006-11-21 10:57ⓜ
조회수
11,453

아래와 같이 tbl_a라는 테이블이 있습니다.

tbl_a

--------

seq_no 

    1

    1

    2

    3

    .

    .

    .

--------


오라클의 경우는 이런식으로 했습니다.

1. 먼저 중복된 값을 찾는다.

select seq_no,count(seq_no)

   from tbl_a

  group by seq_no

  having by count(seq_no) >1

2. 중복된 값을 rowid가 max인값이거나 (min인값)을 삭제한다.

 delete

   from tbl_a A

 where rowid > ( select min(rowid) 

                          from tbl_a B

                         where b.seq_no = a.seq_no)

    and seq_no = '1';     <-중복된 값중에 삭제하고싶은값


DB2는 한지 얼마 안되서 잘 모르겠더군요. 어떻게 해야 되는지 답변좀 부탁드립니다. 위의 2번의 쿼리를 모르겠습니다. db2에서도 rowid가 있는지 찾아봤는데

보이진 않고 row_number() over()이것이 보이던데..이것으로 대체가 될까요?

이 글에 대한 댓글이 총 2건 있습니다.

select * from t1

C1        
-----------
          1
          1
          2
          2
          2

  5 record(s) selected.


export to t1.ixf of ixf select c1 from ( select c1,rownumber() over(partition by c1 ) rn from t1 ) a where rn=1
SQL3104N  The Export utility is beginning to export data to file "t1.ixf".

SQL3105N  The Export utility has finished exporting "2" rows.


Number of rows exported: 2


import from t1.ixf of ixf create into t2
SQL3150N  The H record in the PC/IXF file has product "DB2    02.00", date
"20061121", and time "102716".

SQL3153N  The T record in the PC/IXF file has name "t1.ixf", qualifier "", and
source "            ".

SQL3109N  The utility is beginning to load data from file "t1.ixf".

SQL3110N  The utility has completed processing.  "2" rows were read from the
input file.

SQL3221W  ...Begin COMMIT WORK. Input Record Count = "2".

SQL3222W  ...COMMIT of any database changes was successful.

SQL3149N  "2" rows were processed from the input file.  "2" rows were
successfully inserted into the table.  "0" rows were rejected.


Number of rows read         = 2
Number of rows skipped      = 0
Number of rows inserted     = 2
Number of rows updated      = 0
Number of rows rejected     = 0
Number of rows committed    = 2


select * from t2

C1        
-----------
          1
          2

  2 record(s) selected.


rename table t1 to t1_old;
rename table t2 to t1;

테이블을 삭제하는 것보다 다시 만드는 것이 성능에는 더 좋습니다.

도님이 2006-11-21 11:33에 작성한 댓글입니다. Edit

우선 좋은답변 감사 드립니다.
그리고 걍 내방식대로 해결해 보았습니다.^^
--중복행 제거
select *
--delete
  from (select seq_no, ROW_NUMBER() OVER (partition by seq_no ORDER BY seq_no) AS seq_id
          FROM tbl_a  
         where loan_no in ( SELECT seq_no
                              FROM tbl_a
                             group by seq_no
                            having count(seq_no)> 1))a
 where a.seq_id > 1;
 

ROW_NUMBER() OVER () 함수를 응용하니까 되네요.

김중인(jagalchi)님이 2006-11-21 13:26에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
1186DB2 트리거나 함수 바로 작성 어떻게 하나요(8.2) [2]
DB2
2006-11-24
8146
1185oracle - iSeriese AS/400 DB2 [1]
DB2처음이
2006-11-23
8356
1184쿼리 좀 봐주세요...~ [1]
왕초보
2006-11-22
8215
1182키 안잡힌 테이블에서 중복 데이터 조회 후 삭제하기 [2]
김중인
2006-11-21
11453
1180db2 개발센터는 어디서 다운 받아 설치하나요? [2]
강성경
2006-11-20
8723
1177DB2의 객체의 종류가 어떻게 되죠? [1]
^^V
2006-11-20
7195
1176runstats 옵션 중 with distribution 옵션의 의미가 궁금합니다. [1]
db2초보
2006-11-17
8030
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.028초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다