Jump to content

Deleting duplicates after joining two tables


Ashu96

Recommended Posts

Hi everyone, I am currently learning SQL and was going through self join tutorial. 

The command for the same was as follows:

image.thumb.png.26cb4b9fec3f16cb7ce631e7cc296a25.png

 

The output is attached below. However there will always be duplicates in this output. How shall I remove these duplicate rows after joining them ?

image.thumb.png.ca09a842f37ed19a90337ea56ea7682d.png

Link to comment
Share on other sites

There's no efficient way to solve it.

Here's one possible solution, but this query is really inefficient because it's doing string operations on every single row.

SELECT A.CustomerName AS CustomerName1, B.CustomerName as CustomerName2, A.City,
CASE
  WHEN A.CustomerName < B.CustomerName THEN CONCAT(A.CustomerName,B.CustomerName)
  ELSE CONCAT(B.CustomerName, A.CustomerName)
END AS DuplicateTest
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
  AND A.City = B.City
GROUP BY DuplicateTest
ORDER BY A.City

 

6 hours ago, niche said:

None of the solutions in that article apply here, because the rows shown in the first post aren't actually duplicates by SQL standards.

The topic creator is saying that a row [ X Y ] is a duplicate of [ Y X ], but the values in each of the two columns are different from one row to the next.


 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...