pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Marlowe <smarlowe@g2switchworks.com>
To: Salman Tahir <salmantahir1@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: cartesian product
Date: Mon, 19 Feb 2007 12:09:40 -0600
Message-ID: <1171908580.25338.5.camel@state.g2switchworks.com> (raw)
In-Reply-To: <2049e51a0702190858n6932461dgc294d05b035aad53@mail.gmail.com>
References: <2049e51a0702190858n6932461dgc294d05b035aad53@mail.gmail.com>

On Mon, 2007-02-19 at 10:58, Salman Tahir wrote:
> Hi,
> 
> I have a query regarding an SQL statement I'm trying to execute. I
> have the following table:
> 
> sequence
> -------------+
> AK
> AKCMK
> CMKA
> 
> I execute the following statement (Cartesian product):
> 
> SELECT p1.sequence as sequence1, p2.sequence as sequence2
> FROM potential_pairs p1, potential_pairs p2
> 
> which gives me:
> 
> sequence1 | sequence2
> ----------------+--------------
>  AK           | AK
>   AK           | AKCMK
>   AK           | CMKA
>   AKCMK    | AK
>   AKCMK    | AKCMK
>   AKCMK    | CMKA
>   CMKA      | AK
>   CMKA      | AKCMK
>   CMKA      | CMKA
> (9 rows)
> 
> I want to eliminate duplicates and by duplicate I mean a tuple such as
> {AK, CMKA} should be regarded as the same as {CMKA, AK}. So I would
> like the following result:
> 
> sequence1 | sequence2
> ----------------+--------------
>  AK           | AK
>   AK           | AKCMK
>   AK           | CMKA
>   AKCMK    | AKCMK
>   AKCMK    | CMKA
>   CMKA      | CMKA

Oh wait, slightly different thing you meant.  OK, you'll want something
like:

select p1.sequence as sequence1, p2.sequence as sequence2 from 
potential_pairs p1, potential_pairs p2 where p1.sequence >= p2.sequence 



view thread (4+ messages)

Message-ID: <1171908580.25338.5.camel@state.g2switchworks.com>
Permalink:  ../1171908580.25338.5.camel@state.g2switchworks.com/
Also on:    postgresql.org/message-id/1171908580.25338.5.camel@state.g2switchworks.com

 · 

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: smarlowe@g2switchworks.com, salmantahir1@gmail.com
  Subject: Re: cartesian product
  In-Reply-To: <1171908580.25338.5.camel@state.g2switchworks.com>

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

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