public inbox for [email protected]  
help / color / mirror / Atom feed
select only 1 pair
7+ messages / 7 participants
[nested] [flat]

* select only 1 pair
@ 2022-10-24 14:44  Shaozhong SHI <[email protected]>
  0 siblings, 5 replies; 7+ messages in thread

From: Shaozhong SHI @ 2022-10-24 14:44 UTC (permalink / raw)
  To: pgsql-sql <[email protected]>

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


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* AW: select only 1 pair
@ 2022-10-24 14:51  Stöcker, Martin <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  4 siblings, 0 replies; 7+ messages in thread

From: Stöcker, Martin @ 2022-10-24 14:51 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; pgsql-sql <[email protected]>

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: [email protected]

[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: [email protected]<mailto:[email protected]>
Von: Shaozhong SHI <[email protected]>
Gesendet: Montag, 24. Oktober 2022 16:44
An: pgsql-sql <[email protected]>
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


Attachments:

  [image/png] image001.png (1.9K, 3-image001.png)
  download | view image

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* RE: select only 1 pair
@ 2022-10-24 14:59  Voillequin, Jean-Marc <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  4 siblings, 0 replies; 7+ messages in thread

From: Voillequin, Jean-Marc @ 2022-10-24 14:59 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; pgsql-sql <[email protected]>

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

should work

From: Shaozhong SHI <[email protected]>
Sent: Monday, October 24, 2022 4:44 PM
To: pgsql-sql <[email protected]>
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.


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: select only 1 pair
@ 2022-10-24 15:01  William Alves Da Silva <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  4 siblings, 1 reply; 7+ messages in thread

From: William Alves Da Silva @ 2022-10-24 15:01 UTC (permalink / raw)
  To: pgsql-sql <[email protected]>; Shaozhong SHI <[email protected]>

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 <[email protected]>, 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


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: select only 1 pair
@ 2022-10-24 15:01  Frank Streitzig <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  4 siblings, 0 replies; 7+ messages in thread

From: Frank Streitzig @ 2022-10-24 15:01 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]>

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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: select only 1 pair
@ 2022-10-24 15:08  Thomas Kellerer <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  4 siblings, 0 replies; 7+ messages in thread

From: Thomas Kellerer @ 2022-10-24 15:08 UTC (permalink / raw)
  To: [email protected]

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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: select only 1 pair
@ 2022-10-24 15:57  Steve Midgley <[email protected]>
  parent: William Alves Da Silva <[email protected]>
  0 siblings, 0 replies; 7+ messages in thread

From: Steve Midgley @ 2022-10-24 15:57 UTC (permalink / raw)
  To: William Alves Da Silva <[email protected]>; +Cc: pgsql-sql <[email protected]>; Shaozhong SHI <[email protected]>

On Mon, Oct 24, 2022 at 8:02 AM William Alves Da Silva <
[email protected]> 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


^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2022-10-24 15:57 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-10-24 14:44 select only 1 pair Shaozhong SHI <[email protected]>
2022-10-24 14:51 ` AW: select only 1 pair Stöcker, Martin <[email protected]>
2022-10-24 14:59 ` Voillequin, Jean-Marc <[email protected]>
2022-10-24 15:01 ` William Alves Da Silva <[email protected]>
2022-10-24 15:57   ` Steve Midgley <[email protected]>
2022-10-24 15:01 ` Frank Streitzig <[email protected]>
2022-10-24 15:08 ` Thomas Kellerer <[email protected]>

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