From: | "Josh Berkus" <josh(at)agliodbs(dot)com> |
---|---|
To: | Jeff Eckermann <jeff_eckermann(at)yahoo(dot)com>, Aasmund Midttun Godal <postgresql(at)envisity(dot)com>, josh(at)agliodbs(dot)com |
Cc: | jeremy(at)wundt(dot)psychiatry(dot)uiowa(dot)edu, pgsql-sql(at)postgresql(dot)org |
Subject: | Re: transposing data for a view |
Date: | 2001-11-01 16:17:00 |
Message-ID: | web-496664@davinci.ethosmedia.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/plain | 2 bytes |
unknown_filename | text/plain | 2 bytes |
unknown_filename | text/plain | 2 bytes |
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Eckermann | 2001-11-01 16:22:05 | Re: transposing data for a view |
Previous Message | David M. Richter | 2001-11-01 16:08:00 | Re: View consistency |