agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: William Alves Da Silva <william_silva@unochapeco.edu.br>
To: pgsql-sql <pgsql-sql@lists.postgresql.org>
To: Shaozhong SHI <shishaozhong@gmail.com>
Subject: Re: select only 1 pair
Date: Mon, 24 Oct 2022 12:01:30 -0300
Message-ID: <7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark> (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>

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|

Regards,

William Alves
On 24 Oct 2022 11:44 -0300, Shaozhong SHI <shishaozhong@gmail.com>, wrote:
> 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

view thread (6+ messages)  latest in thread

Message-ID: <7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark>
Permalink:  ../7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark/
Also on:    postgresql.org/message-id/7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark

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: william_silva@unochapeco.edu.br, pgsql-sql@lists.postgresql.org, shishaozhong@gmail.com
  Subject: Re: select only 1 pair
  In-Reply-To: <7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark>

* 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