Re: select only 1 pair

Lists: pgsql-sql
From: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
To: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: select only 1 pair
Date: 2022-10-24 14:44:03
Message-ID: CA+i5Jwa5VR7vJ=9S-9-d_3HgxQ_MmqwLW9+vqeN=jNDUVSVCHQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

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


From: Stöcker, Martin <Martin(dot)Stoecker(at)etl-datenservice(dot)de>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>, pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: AW: select only 1 pair
Date: 2022-10-24 14:51:26
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

select id1, id2 from table where id1<id2;

Mit freundlichen Grüßen

Martin Stöcker
-----------------------------------------
ETL Datenservice GmbH
Widdersdorfer Str. 415 | D-50933 Köln
Telefon: +49(0)2219544010
Fax: +49(0)2219544015
Email: Martin(dot)Stoecker(at)etl-datenservice(dot)de

[etlds]

ETL Datenservice GmbH
Widdersdorfer Str. 415 · 50933 Köln
Geschäftsführer: Dr. Dirk Goldner, Dennis Küpper, ppa. Melanie Lillich
Amtsgericht Köln · HRB 75439 · USt.-Id: DE 122 805 685
www.etl-datenservice.de<http://www.etl-datenservice.de>
Email: info(at)etl-datenservice(dot)de<mailto:info(at)etl-datenservice(dot)de>
Von: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Gesendet: Montag, 24. Oktober 2022 16:44
An: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Betreff: select only 1 pair

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


From: "Voillequin, Jean-Marc" <Jean-Marc(dot)Voillequin(at)moodys(dot)com>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>, pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: RE: select only 1 pair
Date: 2022-10-24 14:59:56
Message-ID: MN2PR20MB2735EC8F53206F809521AA16BE2E9@MN2PR20MB2735.namprd20.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

select id1,id2 from your_table where id2>=id1
union
select id2,id1 from your_table where id2<id1;

should work

From: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Sent: Monday, October 24, 2022 4:44 PM
To: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: select only 1 pair

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 ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍
ZjQcmQRYFpfptBannerStart
This email originated from outside of Moody's
Do not click links or open attachments unless you recognize the sender and know the content is safe.
ZjQcmQRYFpfptBannerEnd
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

----------------------------------------------------------------------
Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.


From: William Alves Da Silva <william_silva(at)unochapeco(dot)edu(dot)br>
To: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>, Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Subject: Re: select only 1 pair
Date: 2022-10-24 15:01:30
Message-ID: 7f09c27c-c4df-4836-bd29-706ebc5eb154@Spark
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

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(at)gmail(dot)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


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: Whole Thread | Raw Message | Download mbox | Resend email
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


From: Thomas Kellerer <shammat(at)gmx(dot)net>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: select only 1 pair
Date: 2022-10-24 15:08:52
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

Shaozhong SHI schrieb am 24.10.2022 um 16:44:
> 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?

select distinct least(id1,id2), greatest(id1,id2)
from the_table


From: Steve Midgley <science(at)misuse(dot)org>
To: William Alves Da Silva <william_silva(at)unochapeco(dot)edu(dot)br>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>, Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Subject: Re: select only 1 pair
Date: 2022-10-24 15:57:01
Message-ID: CAJexoSJLO31fthcjHqyQ+BoE-WYGq0mfX3fHN0uiWPb6r1VYug@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-sql

On Mon, Oct 24, 2022 at 8:02 AM William Alves Da Silva <
william_silva(at)unochapeco(dot)edu(dot)br> wrote:

> 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|
>
>
>
I'll let OP clarify, but in your dataset example (not the same as
original), shouldn't "4, 1" be found also. It's a unique pair (whereas
"1,2" and "2,1" are repeating when sorted low/high). Which makes me think
that merging the two columns into an array, sorting the array, and then
squasing duplicates would do the job? Maybe there's an easier way, but from
what I can see of the original requirements, your dataset should return the
following?

1 | 2
3 | 4
4 | 1

Steve