Re: select only 1 pair

Frank Streitzig <fstreitzig@gmx.net>

From: Frank Streitzig <fstreitzig@gmx.net>
To: Shaozhong SHI <shishaozhong@gmail.com>
Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
Date: 2022-10-24T15:01:54Z
Lists: pgsql-sql
Am Mon, Oct 24, 2022 at 03:44:03PM +0100 schrieb Shaozhong SHI:
> There are pair ids.  Each pair is repeated.
>
> id1   id2
> 1       2
> 2        1
> 3         4
> 4         3
>
> How to only select 1 unique pair for each?
>
> Regards,
> David

Hello,

if just 2 id's then sort with min and max comparing.
Example:

with data (id1, id2) as (
    values (1,2), (2,1), (3,4), (4,3)
)
select case when id1 <= id2 then id1 else id2 end as idmin
       , case when not id1 <= id2 then id1 else id2 end as idmin
    from data
    group by 1, 2
;

Best regards,
Frank