X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id BE4F3D1B551 for ; Thu, 9 Oct 2003 17:58:29 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 81804-07 for ; Thu, 9 Oct 2003 14:57:40 -0300 (ADT) Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by svr1.postgresql.org (Postfix) with ESMTP id B50F4D1B4FC for ; Thu, 9 Oct 2003 14:57:39 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-31.mail.demon.net with esmtp (Exim 3.35 #1) id 1A7f2h-000NkD-0V; Thu, 09 Oct 2003 18:57:40 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id E2FD517453; Thu, 9 Oct 2003 18:57:38 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 0BD0D173F1; Thu, 9 Oct 2003 18:57:38 +0100 (BST) From: Richard Huxton To: Josh Berkus , Kurt Overberg , pgsql-sql@postgresql.org Subject: Re: Selecting rows as if they were columns? Date: Thu, 9 Oct 2003 18:57:37 +0100 User-Agent: KMail/1.5 References: <3F855F38.9070803@hotdogrecords.com> <200310091014.09695.josh@agliodbs.com> In-Reply-To: <200310091014.09695.josh@agliodbs.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200310091857.37460.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/105 X-Sequence-Number: 15505 On Thursday 09 October 2003 18:14, Josh Berkus wrote: > Kurt, > > > I've got what I think should be a pretty simple problem- I just can't > > figure out how I'd do it. Heck, I can't even figure out how I'd search > > for an answer to this problem. > > This is an old common SQL problem ... the "crosstab" table. There are 3 > standard ways to solve it, depending on the exact circumstances of your > case. It's probably best if you buy Joe Celko's "SQL for Smarties, 2nd > Ed.", which has a 50-page chapter on the topic and explores the methods in > detail. Or see the contrib/tablefunc functions which can do this sort of thing for you. -- Richard Huxton Archonet Ltd