Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / August 2006

Tip: Looking for answers? Try searching our database.

Finding Duplicate Records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carroll - 30 Aug 2006 16:14 GMT
I'm looking for a way in SQL to find duplicate records in a single
table, that are the same based on 3 columns, regardless of what is in
the other columns in the duplicate records.  I would like to keep both
records (or it could be more than 2 as well) where duplicate records
are found.  Also, I am interested in selecting all columns from the
duplicate records.

Thanks,

Carroll Rinehart
Serge Rielau - 30 Aug 2006 16:40 GMT
> I'm looking for a way in SQL to find duplicate records in a single
> table, that are the same based on 3 columns, regardless of what is in
> the other columns in the duplicate records.  I would like to keep both
> records (or it could be more than 2 as well) where duplicate records
> are found.  Also, I am interested in selecting all columns from the
> duplicate records.

SELECT cnt, c1, c2, c3, c4, c5
(SELECT COUNT(1) OVER(PARTITION BY c1, c2, c3) AS cnt,
       c1, c2, c3, c4, c5
FROM T) AS S
WHERE cnt > 1;

If you want to learn how to remove:
IOD Coneference
TLU-1288A    SQL on Fire!
Oct 17 02:30 PM - 05:30 PM
Convention Center - 206A
seating is limited. ;-)

Or IDUG 2007 Europe in Vienna in 4 short weeks....
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ondemandbusiness/conf2006/

Brian Tkatch - 30 Aug 2006 17:48 GMT
> I'm looking for a way in SQL to find duplicate records in a single
> table, that are the same based on 3 columns, regardless of what is in
[quoted text clipped - 6 lines]
>
> Carroll Rinehart

SELECT * FROM table
WHERE (Col1, Col2, Col3) IN
(
SELECT Col1, Col2, Col3 FROM table
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1
)

OR

SELECT * FROM table OUTER
WHERE EXISTS
(
SELECT Col1, Col2, Col3 FROM table INNER
WHERE INNER.Col1 = OUTER.Col1
    AND INNER.Col2 = OUTER.Col2
    AND INNER.Col3 = OUTER.Col3
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1
)

B.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.