From: Michael Glaesemann <grzm@myrealbox.com>
To: David Witham <davidw@unidial.com.au>
Cc: pgsql-sql@postgresql.org
Subject: Re: Transpose rows to columns
Date: Tue, 13 Jan 2004 12:50:54 +0900
Message-ID: <B032D732-457B-11D8-B881-000A95C88220@myrealbox.com> (raw)
In-Reply-To: <CFA248776934FD43847E740E43C346D199DB64@ozimelb03.ozicom.com>
References: <CFA248776934FD43847E740E43C346D199DB64@ozimelb03.ozicom.com>
Hi David
On Jan 13, 2004, at 10:12 AM, David Witham wrote:
> Hi,
>
> I have a query that returns data like this:
>
> cust_id cust_name month cost revenue margin
> 991234 ABC 2003-07-01 10 15 5
> 991234 ABC 2003-08-01 11 17 6
> 991234 ABC 2003-09-01 12 19 7
> 991235 XYZ 2003-07-01 13 21 8
> 991235 XYZ 2003-08-01 12 19 7
> 991235 XYZ 2003-09-01 11 17 6
>
> I want to turn it around so it displays like this:
>
> 991234,ABC,2003-07-01,10,15,5,2003-08-01,11,17,6,2003-09-01,12,19,7
> 991235,XYZ,2003-07-01,13,21,8,2003-08-01,12,19,7,2003-09-01,11,17,6
>
> (I've used commas to shorten the layout for the example)
>
> Does anyone have some ideas on how to do this?
I'd suggest looking at tablefunc in /contrib. It includes crosstab
functionality that you might find useful. I don't think it'll do
exactly what you describe here, but something quite similar.
Michael Glaesemann
grzm myrealbox com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: grzm@myrealbox.com, davidw@unidial.com.au
Subject: Re: Transpose rows to columns
In-Reply-To: <B032D732-457B-11D8-B881-000A95C88220@myrealbox.com>
References: <CFA248776934FD43847E740E43C346D199DB64@ozimelb03.ozicom.com> <B032D732-457B-11D8-B881-000A95C88220@myrealbox.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox