| |
|
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.
|
 |

02-09-04, 12:00
|
|
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?
|
|

02-09-04, 13:16
|
|
:-)
|
|
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;
|
|

02-09-04, 13:41
|
|
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.
|
|

02-09-04, 14:55
|
|
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
|
|

02-09-04, 15:46
|
|
:-)
|
|
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
...
|
|

02-09-04, 16:34
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|