Home > Mobile >  How to get distinct combination of three columns regardless of order?
How to get distinct combination of three columns regardless of order?

Time:12-16

I have a table (tablename) with three columns:

c1 c2 c3
A B C
A C B
A B D

I want the output as:

c1 c2 c3
A B C
A B D

That is, I want the unique combination of values from three columns regardless of which column they are in

Note: The above values are just as an example. My table contains many order combinations

CodePudding user response:

You could convert the columns to an array, sort it, take out the elements, and then use DISTINCT.

SELECT DISTINCT
  c_array[0] AS c1,
  c_array[1] AS c2,
  c_array[2] AS c3
FROM
(
  SELECT sort_array(array(c1, c2, c3)) AS c_array FROM tablename
)
  collapsed

CodePudding user response:

select * from tblname where c1='A' and c2='B'

you can get the output as mentioned above by using this where condition

  • Related