Transpose rows to columns

From: "David Witham" <davidw(at)unidial(dot)com(dot)au>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Transpose rows to columns
Date: 2004-01-13 01:12:52
Message-ID: CFA248776934FD43847E740E43C346D199DB64@ozimelb03.ozicom.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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?

Thanks,

David Witham
Telephony Platforms Architect
Unidial
Ph: 03 8628 3383
Fax: 03 8628 3399

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2004-01-13 03:50:54 Re: Transpose rows to columns
Previous Message Bronx 2004-01-12 23:58:52 Is it possible in PostgreSQL?