agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Frank Streitzig <fstreitzig@gmx.net>
To: Shaozhong SHI <shishaozhong@gmail.com>
Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
Subject: Re: select only 1 pair
Date: Mon, 24 Oct 2022 17:01:54 +0200
Message-ID: <Y1ao4uH2OG8d5N96@frastr-dev> (raw)
In-Reply-To: <CA+i5Jwa5VR7vJ=9S-9-d_3HgxQ_MmqwLW9+vqeN=jNDUVSVCHQ@mail.gmail.com>
References: <CA+i5Jwa5VR7vJ=9S-9-d_3HgxQ_MmqwLW9+vqeN=jNDUVSVCHQ@mail.gmail.com>

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





view thread (6+ messages)  latest in thread

Message-ID: <Y1ao4uH2OG8d5N96@frastr-dev>
Permalink:  ../Y1ao4uH2OG8d5N96@frastr-dev/
Also on:    postgresql.org/message-id/Y1ao4uH2OG8d5N96@frastr-dev

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: fstreitzig@gmx.net, shishaozhong@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: Re: select only 1 pair
  In-Reply-To: <Y1ao4uH2OG8d5N96@frastr-dev>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox