Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id D4C2A9FB811 for ; Mon, 19 Feb 2007 14:07:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 95703-05 for ; Mon, 19 Feb 2007 14:07:39 -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 290C69FB7CE for ; Mon, 19 Feb 2007 14:07:39 -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:07:32 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 19 Feb 2007 12:07:32 -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: <1171908452.25338.2.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:07:32 -0600 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.019 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_PASS, UNPARSEABLE_RELAY X-Spam-Level: X-Archive-Number: 200702/201 X-Sequence-Number: 27535 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