Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E9E879FB8C1 for ; Mon, 19 Feb 2007 14:09:45 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 00997-01-4 for ; Mon, 19 Feb 2007 14:09:42 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 94A509FB8A2 for ; Mon, 19 Feb 2007 14:09:41 -0400 (AST) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Mon, 19 Feb 2007 18:09:40 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 19 Feb 2007 12:09:40 -0600 Subject: Re: cartesian product From: Scott Marlowe To: Salman Tahir Cc: pgsql-sql@postgresql.org In-Reply-To: <2049e51a0702190858n6932461dgc294d05b035aad53@mail.gmail.com> References: <2049e51a0702190858n6932461dgc294d05b035aad53@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1171908580.25338.5.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Mon, 19 Feb 2007 12:09:40 -0600 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.018 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_PASS, UNPARSEABLE_RELAY X-Spam-Level: X-Archive-Number: 200702/202 X-Sequence-Number: 27536 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