Re: select only 1 pair

From: Frank Streitzig <fstreitzig(at)gmx(dot)net>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: select only 1 pair
Date: 2022-10-24 15:01:54
Message-ID: Y1ao4uH2OG8d5N96@frastr-dev
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2022-10-24 15:08:52 Re: select only 1 pair
Previous Message William Alves Da Silva 2022-10-24 15:01:30 Re: select only 1 pair