X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id AAC05474E4F for ; Fri, 21 Mar 2003 22:59:18 -0500 (EST) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 7B0E7474E42 for ; Fri, 21 Mar 2003 22:59:18 -0500 (EST) Received: by news.hub.org (Postfix, from userid 8) id 57AE643A611; Fri, 21 Mar 2003 22:59:18 -0500 (EST) From: Lee Harr X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Diffcult query Date: Sat, 22 Mar 2003 03:59:18 +0000 (UTC) Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 36 Message-ID: References: Reply-To: missive@hotmail.com X-Complaints-To: usenet@news.hub.org User-Agent: slrn/0.9.7.3 (FreeBSD) To: pgsql-sql@postgresql.org X-Spam-Status: No, hits=-22.8 required=5.0 tests=EMAIL_ATTRIBUTION,QUOTED_EMAIL_TEXT,REFERENCES, REPLY_WITH_QUOTES,USER_AGENT autolearn=ham version=2.50-cvs X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50-cvs (1.172-2003-02-14-exp) X-Archive-Number: 200303/516 X-Sequence-Number: 12555 In article , Dan Winslow wrote: > Hi folks. This is giving me fits. I feel like it oughta be simple, but > apparantly its not...I think. > > Given a table : > > create table tablea ( > code1 varchar(32), > code2 varchar(32), > cost int > ); > > and the rows > > code1 code2 cost > ---------------------------------- > "aaa" "bbb" 2 > "ddd" "eee" 3 > "bbb" "aaa" 6 > "ggg" "hhh" 4 > > I need a ( preferably single ) query that will sum the costs for all unique > pairs of codes, without regard to column order. That is, for summing > purposes, rows 1 and 3 are identical, and should produce the sum of 8 for > the unordered pair("aaa","bbb"). It should also, of course, prevent the case > where ("bbb","aaa") is considered a seperate pair. Any ideas would be much > appreciated. > > Maybe you could create a function that will return all of the rows with a particular pair of codes. Then I think you could GROUP BY that function...