X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5AA0FD1B447 for ; Tue, 13 Jan 2004 03:51:27 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 37182-01 for ; Mon, 12 Jan 2004 23:50:59 -0400 (AST) Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [192.108.102.143]) by svr1.postgresql.org (Postfix) with ESMTP id 261B2D1B43A for ; Mon, 12 Jan 2004 23:50:55 -0400 (AST) Received: from [10.10.14.130] grzm@smtp-send.myrealbox.com [61.115.206.98] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 3.48 $ on Novell NetWare; Mon, 12 Jan 2004 20:50:55 -0700 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v609) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: From: Michael Glaesemann Subject: Re: Transpose rows to columns Date: Tue, 13 Jan 2004 12:50:54 +0900 To: "David Witham" X-Mailer: Apple Mail (2.609) X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/101 X-Sequence-Number: 16494 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