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

From Frank Streitzig
Subject Re: select only 1 pair
Date
Msg-id Y1ao4uH2OG8d5N96@frastr-dev
Whole thread Raw
In response to select only 1 pair  (Shaozhong SHI <shishaozhong@gmail.com>)
List 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



pgsql-sql by date:

Previous
From: William Alves Da Silva
Date:
Subject: Re: select only 1 pair
Next
From: Thomas Kellerer
Date:
Subject: Re: select only 1 pair