Ben Nicholl
Ben Nicholl

Reputation: 33

SQL select distinct combination of values on multiple columns

I've checked everywhere for a simple solution to this and I can not find it. Say you have a database

column A column B
function A function B
function B function A
function C function D
function D function C
function E function F
function H function G

I want to get rid of the combination of unique values, so I output this

column A column B
function A function B
function C function D
function E function F
function H function G

Is there an elegant way to achieve this?

Upvotes: 0

Views: 1155

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270873

You can use <:

select t.*
from t
where column_a < column_b;

This will select one row from each reversed pair, the one where column_a has the lower value.

EDIT:

The question changed after I answered it. That is pretty rude, but I'll adjust the answer anyway:

select t.*
from t
where column_a < column_b or 
      not exists (select 1
                  from t t2
                  where t2.column_a = t.column_b and
                        t2.column_b = t.column_a
                 );

Upvotes: 2

Related Questions