Re: select only 1 pair

From: Steve Midgley <science(at)misuse(dot)org>
To: William Alves Da Silva <william_silva(at)unochapeco(dot)edu(dot)br>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>, Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Subject: Re: select only 1 pair
Date: 2022-10-24 15:57:01
Message-ID: CAJexoSJLO31fthcjHqyQ+BoE-WYGq0mfX3fHN0uiWPb6r1VYug@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Oct 24, 2022 at 8:02 AM William Alves Da Silva <
william_silva(at)unochapeco(dot)edu(dot)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 (id1, id2)
>
> id1 |id2 |
> ------+------+
> 1| 2|
> 2| 1|
> 3| 4|
> 4| 1|
>
>
> I think that is what you need
> *SELECT* *DISTINCT* *LEAST*(id1, id2) *AS* id1, *GREATEST*(id1, id2) *AS*
> id2
> *FROM* (*VALUES* (1, 2), (2, 1), (3, 4), (4, 3)) t (id1, id2)
>
> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2022-10-31 17:04:00 access sub elements using any()
Previous Message Thomas Kellerer 2022-10-24 15:08:52 Re: select only 1 pair