X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 57B14475A8D for ; Mon, 24 Mar 2003 03:19:25 -0500 (EST) Received: from shield.pirmabanka.lv (unknown [195.114.57.4]) by postgresql.org (Postfix) with ESMTP id 726284758F1 for ; Mon, 24 Mar 2003 03:19:23 -0500 (EST) Received: from wall2.pirmabanka.lv (wall2-pub.pirmabanka.lv [195.114.57.14]) by shield.pirmabanka.lv (8.12.8/8.9.3) with SMTP id h2O8JMtw028637 for ; Mon, 24 Mar 2003 10:19:22 +0200 Received: from share.pirmabanka.lv ([10.0.0.10]) by wall2.pirmabanka.lv; Mon, 24 Mar 2003 10:19:21 +0200 (EET) Received: from pc311x1.pirmabanka.lv (localhost.localdomain [127.0.0.1]) by pirmabanka.lv (8.11.6/8.11.6) with SMTP id h2O8JLY03285 for ; Mon, 24 Mar 2003 10:19:21 +0200 Received: (qmail 23658 invoked by uid 100); 24 Mar 2003 08:20:14 -0000 Date: Mon, 24 Mar 2003 10:20:14 +0200 From: "Victor Yegorov" To: "Dan Winslow" Cc: pgsql-sql@postgresql.org Subject: Re: Seeking help with a query.... Message-ID: <20030324082014.GG20481@pirmabanka.lv> Mail-Followup-To: "Dan Winslow" , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="hcut4fGOf7Kh6EdG" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.1i X-Spam-Status: No, hits=-37.8 required=5.0 tests=BAYES_10,IN_REP_TO,PGP_SIGNATURE_2,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES,USER_AGENT_MUTT autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/536 X-Sequence-Number: 12575 --hcut4fGOf7Kh6EdG Content-Type: text/plain; charset=koi8-r Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * Dan Winslow [21.03.2003 21:58]: > Hi folks, seeking help with a query that I thought was simple, but > apparantly isn't, at least for someone with my knowledge level. >=20 > Given a table : >=20 > create table atable ( > code1 char, > code2 char, > cost int > ); >=20 > And the rows >=20 > code1 code2 cost > ----------------------------- > a b 2 > d e 4 > b a 6 > f g 1 >=20 > I need a ( preferably single ) query that will sum the costs for any > matching pairs of codes regardless of order. That is, row 1 and row 3 > concern the same pair of unordered codes (a,b), and the result should show > that the (a,b) pair had a summed cost of 8. I am not able to change any of > the environment or preconditions other than the query itself. I have tried > so many approaches that they aren't even worth listing. Any suggestions > would be very much appreciated. First thing, that came to my mind: Give each code (if they are not numeric) a number. For this example, that could be ASCII code of chars. Create view on that table: CREATE VIEW aview AS SELECT at.*, code(at.code1) + code(at.code2) AS dbl_code FROM atable at; dbl_code field will have equal values for all groups of codes with same codes involved: a and b, b and a. About function code() I used to create the View: it's just an assumption, you should write one yourself (on C for faster perfomance). Or simply use: code1 + code if your codes are of numeric type and are foreign keys to some other table's primary key. Please, give some feedback on usability of this solution. --=20 Victor Yegorov --hcut4fGOf7Kh6EdG Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE+fr++2RKGLEykUDYRAiPPAKCk1r3vCwJYQaDILaL3sAJTD1WqRACfTNHR o+mhsoy1y9deXJaktwSEI7k= =t15P -----END PGP SIGNATURE----- --hcut4fGOf7Kh6EdG--