pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
cartesian product
4+ messages / 3 participants
[nested] [flat]

* cartesian product
@ 2007-02-19 16:58  Salman Tahir <salmantahir1@gmail.com>
  0 siblings, 3 replies; 4+ messages in thread

From: Salman Tahir @ 2007-02-19 16:58 UTC (permalink / raw)
  To: pgsql-sql

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

Any help would be appreciated.

- Salman



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

* Re: cartesian product
@ 2007-02-19 17:56  Luiz K. Matsumura <luiz@planit.com.br>
  parent: Salman Tahir <salmantahir1@gmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Luiz K. Matsumura @ 2007-02-19 17:56 UTC (permalink / raw)
  To: Salman Tahir <salmantahir1@gmail.com>; +Cc: pgsql-sql

Hi, Salman

Maybe this isn't so much elegant, but works:

SELECT p1.sequence as sequence1, p2.sequence as sequence2
FROM potential_pairs p1, potential_pairs p2
WHERE p1.sequence <= p2.sequence

Hope this helps


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
>
> Any help would be appreciated.
>
> - Salman
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>       choose an index scan if your joining column's datatypes do not
>       match
>
>

-- 
Luiz K. Matsumura
Plan IT Tecnologia Informática Ltda.




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

* Re: cartesian product
@ 2007-02-19 18:07  Scott Marlowe <smarlowe@g2switchworks.com>
  parent: Salman Tahir <salmantahir1@gmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Scott Marlowe @ 2007-02-19 18:07 UTC (permalink / raw)
  To: Salman Tahir <salmantahir1@gmail.com>; +Cc: 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
> 


SELECT p1.sequence as sequence1, p2.sequence as sequence2 FROM
potential_pairs p1, join potential_pairs p2 on
(p1.sequence<>p2.sequence) where sequence1 > sequence2




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

* Re: cartesian product
@ 2007-02-19 18:09  Scott Marlowe <smarlowe@g2switchworks.com>
  parent: Salman Tahir <salmantahir1@gmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Scott Marlowe @ 2007-02-19 18:09 UTC (permalink / raw)
  To: Salman Tahir <salmantahir1@gmail.com>; +Cc: 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 



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


end of thread, other threads:[~2007-02-19 18:09 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-02-19 16:58 cartesian product Salman Tahir <salmantahir1@gmail.com>
2007-02-19 17:56 ` Luiz K. Matsumura <luiz@planit.com.br>
2007-02-19 18:07 ` Scott Marlowe <smarlowe@g2switchworks.com>
2007-02-19 18:09 ` Scott Marlowe <smarlowe@g2switchworks.com>

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