If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Any SQL statements for checking duplicate data in a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-04, 04:25
a7man a7man is offline
Registered User
 
Join Date: May 2004
Posts: 7
Any SQL statements for checking duplicate data in a table

Hi,

I wonder if there any syntax or SQL statements that I could use to check for duplicate data in a table (e.g. temp table) ? Thks all in advance !!!
Reply With Quote
  #2 (permalink)  
Old 05-10-04, 08:17
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
A really simple way would be to have DB2 count them.
SELECT cola,colb,colc,...,count(*) as qty from mytable group by cola,colb,colc,...

Those rows in the resultset with a QTY > 1 have duplicate rows.

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 05-10-04, 08:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by ARWinner
Those rows in the resultset with a QTY > 1 have duplicate rows.
andy, your sql is fine, but your closing statement is not

those rows in the resultset with qty > 1 have duplicate selected columns in the table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-10-04, 08:30
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If all the columns are in the group by clause, then it would count the duplicate rows. That is why I used the ellipse (...) to signify that all columns should be specified.

Andy
Reply With Quote
  #5 (permalink)  
Old 05-10-04, 08:36
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
To be more specific:

SELECT cola, colb, colc, count(*)
from mytable
group by cola, colb, colc
having count(*) > 1
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 05-10-04, 23:16
a7man a7man is offline
Registered User
 
Join Date: May 2004
Posts: 7
thks guys for all the help
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On