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 > How to compare 2 records in the same table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-09-04, 12:00
mark_tim1999 mark_tim1999 is offline
Registered User
 
Join Date: Dec 2003
Posts: 14
How to compare 2 records in the same table?

Dear Friends,
Could anyone let me know how to search for duplicate recorsd in the same table?
I have 20 attributes in the table and have the first 2 attributes
ID1,ID2-the combination of which is unique.
How to write a query to retrieve the duplicate records in the same table(It should compare the remaning 18 attributes and retrieve if they all match).Is this something which can be done in SQL?
Reply With Quote
  #2 (permalink)  
Old 02-09-04, 13:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: How to compare 2 records in the same table?

Quote:
Originally posted by mark_tim1999
Dear Friends,
Could anyone let me know how to search for duplicate recorsd in the same table?
I have 20 attributes in the table and have the first 2 attributes
ID1,ID2-the combination of which is unique.
How to write a query to retrieve the duplicate records in the same table(It should compare the remaning 18 attributes and retrieve if they all match).Is this something which can be done in SQL?
The following will retrieve all records that have duplicate ID1 and ID2:

SELECT ID1, ID2, ..., COUNT(1) FROM table_name
GROUP BY ID1, ID2
HAVING COUNT(1) > 1;
Reply With Quote
  #3 (permalink)  
Old 02-09-04, 13:41
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: How to compare 2 records in the same table?

N_I's query gives the count ,

If you want the actual rows, you can use a CTE using the same query as sample

with temp as
(
SELECT ID1, ID2, ..., COUNT(1) FROM table_name
GROUP BY ID1, ID2
HAVING COUNT(1) > 1
)
select * from table_name where
(id1,id2....) in (select id1,id2 .... from temp
)

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 02-09-04, 14:55
mark_tim1999 mark_tim1999 is offline
Registered User
 
Join Date: Dec 2003
Posts: 14
Thanks for the response....
But I dont have duplicate ID1,ID2...This combinaton is unique

Imagine the sample records

ID1 ID2 NAME SSN BIRTHDATE
100 1 MARK 999-99-999 1980-01-01
100 2 MARK 999-99-999 1980-01-01
200 2 CHRIS 888-88-8888 1990-01-01

I am looking for a query which compares the two records
(100,1) & (100,2) for the NAME,SSN,BIRTHDATE(LIKEWISE I have 20 other columns in this table) and gives the count of records if all the attributes match(apart from ID1,ID2)....Hope my question was clear
Reply With Quote
  #5 (permalink)  
Old 02-09-04, 15:46
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by mark_tim1999
Thanks for the response....
But I dont have duplicate ID1,ID2...This combinaton is unique

Imagine the sample records

ID1 ID2 NAME SSN BIRTHDATE
100 1 MARK 999-99-999 1980-01-01
100 2 MARK 999-99-999 1980-01-01
200 2 CHRIS 888-88-8888 1990-01-01

I am looking for a query which compares the two records
(100,1) & (100,2) for the NAME,SSN,BIRTHDATE(LIKEWISE I have 20 other columns in this table) and gives the count of records if all the attributes match(apart from ID1,ID2)....Hope my question was clear

...
GROUP BY NAME, SSN, BIRTHDATE
...
Reply With Quote
  #6 (permalink)  
Old 02-09-04, 16:34
aloz aloz is offline
Registered User
 
Join Date: May 2003
Location: San Juan, PR
Posts: 18
Try this statement:

SELECT A.ID1, A.ID2
FROM TBL A
WHERE 1 < (SELECT COUNT(*) FROM TBL B
WHERE B.FLD1 = A.FLD1
AND B.FLD2 = A.FLD2
AND B.FLD3 = A.FLD3
AND ... )

Regards, ... Aloz
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