Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Lets say I have a table that has INDID, Name, Date_of_birth, Place_of_birth, Date_of_death, Place_of_death.

how can I get duplicate records and the original record that was matched?

example:

1 jon jones 1/20/1920 some place 12/20/2020 some place
99 jon jones 1/20/1920 some place 12/20/2020 some place

if I use row over partition being > 1 I will get the individual with ID number 99. But I want to see both records. I cant use >=1 then that will show all records.

WITH cte AS (
    SELECT 
        Name, 
        Date_of_birth, 
        Place_of_birth, 
        Date_of_death, 
        Place_of_death, 
        ROW_NUMBER() OVER (
            PARTITION BY Name, Date_of_birth, Place_of_birth, Date_of_death, Place_of_death
            ORDER BY Name) rownum
    FROM 
        t1
) 
SELECT 
  * 
FROM 
    cte 
WHERE 
    rownum > 1;


What I have tried:

I tried looking for duplicate match syntax for sql and nothing fruitful came from it.
Posted
Updated 22-May-21 22:30pm

Try using a GROUP BY, and then JOIN those results to your original table.
Assume we have this table:
ID  FName   SName   Email
1   Joe     Smith   Joe@xxx.com                                       
2   Mike    Jones   Mike@xxx.com                                      
3   Joe     Smith   Joe@xxx.com                                       
4   Mike    Smith   Mike.S@xxx.com  

If we use GROUP BY, We can add a count of the rows:
SQL
SELECT [FName]
      ,[SName]
      ,[Email]
      ,COUNT(*) AS Cnt
  FROM [Testing].[dbo].[MyTable]
  GROUP BY [FName]
          ,[SName]
          ,[Email]

FName   SName   Email           Cnt
Joe     Smith   Joe@xxx.com       2
Mike    Jones   Mike@xxx.com      1
Mike    Smith   Mike.S@xxx.com    1
SO it's simple to convert that to just give duplicates:
SQL
SELECT [FName]
      ,[SName]
      ,[Email]
  FROM [Testing].[dbo].[MyTable]
  GROUP BY [FName]
          ,[SName]
          ,[Email]
  HAVING COUNT(*) > 1
:
FName   SName   Email           
Joe     Smith   Joe@xxx.com

Now all you have to do is JOIN them back together to get the IDs:
SQL
SELECT a.* FROM Testing.dbo.MyTable a
JOIN (SELECT [FName]
            ,[SName]
            ,[Email]
        FROM [Testing].[dbo].[MyTable]
        GROUP BY [FName]
                ,[SName]
                ,[Email]
        HAVING COUNT(*) > 1) b
      ON     a.Email = b.Email
         AND a.FName = b.FName
         AND a.SName = b.SName
ID  FName   SName   Email
1   Joe     Smith   Joe@xxx.com                                      
3   Joe     Smith   Joe@xxx.com  
 
Share this answer
 
Comments
Member 11856456 23-May-21 3:53am    
Griff, its not 100% its bringing in records that seem to have no match please see the example:

Last name First name Middle name Verify or Proof code Death month Death day Death year
LEE PEARL 09 00 1975
MARTIN MICHAEL 10 00 1974
SWEET HELEN 07 00 1979
WARD GEORGE 10 00 1982
SMITH JOHN M V 06 15 1967
PEARSON ARTHUR 07 00 1987
PEARSON ARTHUR 07 00 1987

As you can see the last 2 matched but the ones above did not have a match.

code:

SELECT a.* FROM ssdm a
JOIN (SELECT [First name]
,[Last name]
,[Death month]
,[Death day]
,[Death year]
FROM ssdm
GROUP BY [First name]
,[Last name]
,[Death month]
,[Death day]
,[Death year]
HAVING COUNT(*) > 1) b
ON a.[First name] = b.[First name]
AND a.[Last name] = b.[Last name]
AND a.[Death month] = b.[Death month]
and a.[Death day] = b.[Death day]
and a.[Death year] = b.[Death year]
OriginalGriff 23-May-21 6:10am    
Without your actual DB, I can;t test it - and all I can say is "it works for me".
Try extracting just GROUP BY section as a separate query without the HAVING clause, and return it's results including the count to see what that gives you.
One variation is to use EXISTS clause. Consider the following example
SQL
SELECT *
FROM YourTable yt
WHERE EXISTS (SELECT 1
              FROM YourTable yt2
              WHERE yt2.IndId          != yt.IndId
              AND   yt2.Name           =  yt.Name
              AND   yt2.Date_of_birth  =  yt.Date_of_birth
              AND   yt2.Place_of_birth =  yt.Place_of_birth
              AND   yt2.Date_of_death  =  yt.Date_of_death
              AND   yt2.Place_of_death =  yt.Place_of_death)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900