Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fA1GH6P54308 for ; Thu, 1 Nov 2001 11:17:06 -0500 (EST) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.7) with HTTP id 496664; Thu, 01 Nov 2001 08:17:00 -0800 From: "Josh Berkus" Subject: Re: transposing data for a view To: Jeff Eckermann , Aasmund Midttun Godal , josh@agliodbs.com Cc: jeremy@wundt.psychiatry.uiowa.edu, pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.7 Date: Thu, 01 Nov 2001 08:17:00 -0800 Message-ID: In-Reply-To: <20011101154744.98632.qmail@web20802.mail.yahoo.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="_===496664====davinci.ethosmedia.com===_" X-Archive-Number: 200111/9 X-Sequence-Number: 5395 This is a multi-part MIME message --_===496664====davinci.ethosmedia.com===_ Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit Jeff, > SELECT > scanid, > sum ( CASE WHEN region = 'A' THEN volume ELSE NULL > ) > AS A_volume, > sum ( CASE WHEN region = 'B' THEN volume ELSE NULL > ) > AS B_volume, > sum ( CASE WHEN region = 'C' THEN volume ELSE NULL > ) > AS C_volume > FROM table > GROUP BY scanid; Hey, that's an elegant solution to doing it in 7.0.3. I hadn't thought of it. Jeremy, never mind what I said about being forced to upgrade. Upgrading *would* still be a good idea, of course. Of couse, it's only *half* a solution. Your query will result in: scanid A_volume B_volume C_volume 1 34.5 1 55.1 1 12.3 2 11.1 etc. For the second half of the solution, Jeremy needs to create the above as a view ('volume_rollup_1') and apply this second view: SELECT scanid, SUM(A_volume) as A_volume, SUM(B_Volume) as B_Volume, SUM(C_volume) as C_volume FROM volume_rollup_1; This will give Jeremy the "pivot" grid he's looking for. > BTW, I don't believe the self-join approach proposed > earlier will work, because joining on "scanid" will > create a cartesian type join where the region values > will be duplicated (multiplicated!). Not if you're talking about my query, they won't. I use that query form in many projects to create roll-ups; it's the "best" SQL92 approach to the "pivot table" problem. However, it will not work in 7.0.3. -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco --_===496664====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===496664====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===496664====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===496664====davinci.ethosmedia.com===_--