Re: cartesian product

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Salman Tahir <salmantahir1(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: cartesian product
Date: 2007-02-19 18:09:40
Message-ID: 1171908580.25338.5.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Phillip Smith 2007-02-19 22:25:11 DISTINCT ON not working...?
Previous Message Scott Marlowe 2007-02-19 18:07:32 Re: cartesian product