Raoul
Raoul

Reputation: 11

sql: select distinct couple(distinct relation) from two column of same data type

I have two column of the same type in a table:

col1    | col2
--------+---------
AAA     |    BBB
BBB     |    AAA
CCC     |    DDD
DDD     |    CCC
EEE     |    FFF

I want to write a request for a result like his:

col1    | col2
--------+---------
AAA     |    BBB
CCC     |    DDD
EEE     |    FFF

I tried all I know, but no good result

Help please!

Upvotes: 1

Views: 355

Answers (1)

Tom H
Tom H

Reputation: 47464

This works given your sample data. You might need to account for NULLs.

SELECT DISTINCT
    CASE WHEN col1 < col2 THEN col1 ELSE col2 END,
    CASE WHEN col1 < col2 THEN col2 ELSE col1 END
FROM
    My_Table

Upvotes: 2

Related Questions