X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0BF0A565F0 for ; Tue, 22 Mar 2005 00:46:18 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61786-10 for ; Tue, 22 Mar 2005 00:46:16 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id 5318056539 for ; Tue, 22 Mar 2005 00:46:15 +0000 (GMT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 21 Mar 2005 18:46:17 -0600 Subject: Re: "Flattening" query result into columns From: Scott Marlowe To: Thomas Borg Salling Cc: pgsql-sql@postgresql.org In-Reply-To: <20050321215723.E79E356388@svr1.postgresql.org> References: <20050321215723.E79E356388@svr1.postgresql.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-Id: <1111452377.26897.487.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Mon, 21 Mar 2005 18:46:17 -0600 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/294 X-Sequence-Number: 20773 On Mon, 2005-03-21 at 15:57, Thomas Borg Salling wrote: > I am looking for a way to =E2=80=9Dflatten=E2=80=9D a query result, so th= at rows are > =E2=80=9Dtransposed=E2=80=9D into columns, just as asked here for oracle: >=20 > http://groups.google.dk/groups?hl=3Dda&lr=3D&client=3Dfirefox-a&rls=3Dorg= .mozilla:en-US:official&selm=3Daad10be0.0401292322.7b6c320b%40posting.googl= e.com >=20 > =20 >=20 > Is there any way to do this with pgsql ? Here's one from work that allows you to do the same basic thing without a separate cross table: select=20 a.lt , b.perspective as XYZ_pers, b.averageresponsetime as XYZ_aver, b.lowestresponsetime as XYZ_lowe, b.highestresponsetime as XYZ_high, b.totalcount as XYZ_tota, c.perspective as ABC_pers, c.averageresponsetime as ABC_aver, c.lowestresponsetime as ABC_lowe, c.highestresponsetime as ABC_high, c.totalcount as ABC_tota=20 from ( select distinct date_trunc('minutes', lastflushtime) as lt from businessre= questsummary where lastflushtime between '2005-03-14 18:42:34' and '2005-03-21 18:42:34= '=20 and perspective in ('XYZ','ABC') ) as a=20 left join ( select date_trunc('minutes', lastflushtime) as lt, max(perspective) as perspective, floor(avg(averageresponsetime)) as averageresponsetime, min(lowestresponsetime) as lowestresponsetime, max(highestresponsetime) as highestresponsetime, sum(totalcount) as totalcount from businessrequestsummary=20 where perspective =3D'XYZ' group by date_trunc('minutes', lastflushtime) ) as b=20 on=20 (a.lt=3Db.lt)=20 left join ( select date_trunc('minutes', lastflushtime) as lt, max(perspective) as perspective, floor(avg(averageresponsetime)) as averageresponsetime, min(lowestresponsetime) as lowestresponsetime, max(highestresponsetime) as highestresponsetime, sum(totalcount) as totalcount from businessrequestsummary=20 where perspective =3D'ABC' group by date_trunc('minutes', lastflushtime) ) as c=20 on=20 (a.lt=3Dc.lt)=20 IT's generated by a script that makes it as big as we need for all the diff= erent perspectives.