Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f2EN3Hx98025 for ; Wed, 14 Mar 2001 18:03:17 -0500 (EST) (envelope-from olly@lfix.co.uk) Received: from lfix.demon.co.uk ([158.152.59.127] helo=linda.lfix.co.uk) by anchor-post-31.mail.demon.net with esmtp (Exim 2.12 #1) id 14dKIS-000OFi-0V; Wed, 14 Mar 2001 23:03:13 +0000 Received: from lfix.co.uk (olly@localhost [127.0.0.1]) by localhost (8.11.3/8.11.3/Debian 8.11.2-1) with ESMTP id f2EN31G25419; Wed, 14 Mar 2001 23:03:03 GMT Message-Id: <200103142303.f2EN31G25419@linda.lfix.co.uk> X-Mailer: exmh version 2.3.1 01/18/2001 (debian 2.3.1-1) with nmh-1.0.4+dev X-URL: http://www.lfix.co.uk/oliver X-face: "xUFVDj+ZJtL_IbURmI}!~xAyPC"Mrk=MkAm&tPQnNq(FWxv49R}\>0oI8VM?O2VY+N7@F- KMLl*!h}B)u@TW|B}6 cc: pgsql-sql@postgresql.org Subject: Re: PIVOT of data In-reply-to: Message from Srikanth Rao of Wed, 14 Mar 2001 14:29:16 PST. <20010314222916.11494.qmail@web6305.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 14 Mar 2001 23:03:01 +0000 From: "Oliver Elphick" X-Archive-Number: 200103/297 X-Sequence-Number: 1961 Srikanth Rao wrote: >Hi, >I have a table like this: > location | numbrochures | marketing >-----------------------+--------------+----------- > 101 Asheville, NC | 4 | NEWS > 101 Asheville, NC | 1 | TV > 101 Asheville, NC | 3 | RADIO > 101 Asheville, NC | 2 | OTHER > 101 Asheville, NC | 3 | null > 101 Asheville, NC | 1 | TV > 102 'Charlotte', 'NC' | 1 | SIGN > 104 'Colfax', 'NC' | 5 | SIGN > 109 'Moyock', 'NC' | 1 | BROCHURE >(9 rows) > > >I want the headings to be like: > >location | NEWS | TV | RADIO | OTHER | ........ > >How to get this done using sql for postgresql backend? SELECT location, CASE WHEN marketing = 'NEWS' THEN numbrochures ELSE NULL END AS "NEWS", CASE WHEN marketing = 'TV' THEN numbrochures ELSE NULL END AS "TV", ... but it's a clumsy hack and won't work if you don't know the contents of "marketing" in advance. -- Oliver Elphick Oliver.Elphick@lfix.co.uk Isle of Wight http://www.lfix.co.uk/oliver PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47 GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C ======================================== "Let your light so shine before men, that they may see your good works, and glorify your Father which is in heaven." Matthew 5:16