Removing duplicate records in SQL

Muhammed Roshan :

I have a table which is the same as below.

    |----------|---------|
    |  NAME1   |  NAME2  | 
    |----------|---------|   
    |     R1   |   R2    |  
    |     R3   |   R4    |  
    |     R2   |   R1    |  
    |     R1   |   R5    |  
    |     R4   |   R3    |  
    |--------------------|

But Here I need to consider tuples (R1, R2) and (R2, R1) as same. This means only one among the tuples (R1, R2) and (R2, R1) can be in the final table. Similarly, (R4, R3) and (R3, R4) is the same. This means only one among tuples (R3, R4) and (R4, R3) can be in the final table.

Finale table should look like this.

   |----------|---------|
   |  NAME1   |  NAME2  | 
   |----------|---------|   
   |     R1   |   R2    |  
   |     R3   |   R4    |  
   |     R1   |   R5    |  
   |--------------------|

How can I do this with SQL?

Tim Biegeleisen :

You may try using a delete with exists logic:

DELETE
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable t2
              WHERE t2.Name2 = t1.Name1 AND t2.Name1 = t1.Name2) AND
      Name1 > Name2;

The approach here is to delete any tuple which appears with names in reversed order. The first name being lexicographically larger than the second name is also a requirement.

If you instead just want to view your data this way, then use a least/greatest trick:

SELECT DISTINCT
    LEAST(Name1, Name2) AS Name1,
    GREATEST(Name1, Name2) AS Name2
FROM yourTable;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=408001&siteId=1