Re: select only 1 pair - Mailing list pgsql-sql

From Steve Midgley
Subject Re: select only 1 pair
Date
Msg-id CAJexoSJLO31fthcjHqyQ+BoE-WYGq0mfX3fHN0uiWPb6r1VYug@mail.gmail.com
Whole thread Raw
In response to Re: select only 1 pair  (William Alves Da Silva <william_silva@unochapeco.edu.br>)
List pgsql-sql


On Mon, Oct 24, 2022 at 8:02 AM William Alves Da Silva <william_silva@unochapeco.edu.br> wrote:
Hello David,

Try this.

This is an exemple of your table.
SELECT * 
  FROM (VALUES (1, 2), (2, 1), (3, 4), (4, 1)) t (id1id2)
  
id1   |id2   |
------+------+
     1|     2|
     2|     1|
     3|     4|
     4|     1|      
     

I think that is what you need
SELECT DISTINCT LEAST(id1id2AS id1GREATEST(id1id2AS id2
  FROM (VALUES (1, 2), (2, 1), (3, 4), (4, 3)) t (id1id2)

id1   |id2   |
------+------+
     1|     2|
     3|     4|



I'll let OP clarify, but in your dataset example (not the same as original), shouldn't "4, 1" be found also. It's a unique pair (whereas "1,2" and "2,1" are repeating when sorted low/high). Which makes me think that merging the two columns into an array, sorting the array, and then squasing duplicates would do the job? Maybe there's an easier way, but from what I can see of the original requirements, your dataset should return the following?

1 | 2
3 | 4
4 | 1

Steve

pgsql-sql by date:

Previous
From: Thomas Kellerer
Date:
Subject: Re: select only 1 pair
Next
From: Rob Sargent
Date:
Subject: access sub elements using any()