Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4BD12475FE3 for ; Mon, 11 Nov 2002 10:42:36 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 34B39475FD4 for ; Mon, 11 Nov 2002 10:42:35 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Mon Nov 11 16:42:38 2002 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id QAA10543; Mon, 11 Nov 2002 16:42:37 +0100 (MET) Message-ID: <3DCFCFED.1F81C503@rodos.fzk.de> Date: Mon, 11 Nov 2002 16:42:37 +0100 From: Christoph Haller X-Mailer: Mozilla 4.06 [en] (X11; I; HP-UX B.10.20 9000/780) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: lud_nowhere_man@yahoo.com Subject: Re: Generating a cross tab (pivot table) Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/164 X-Sequence-Number: 10306 > I saw something that might somewhat a bit more > flexible solution using SQL. I don't know if it works > in PostgreSQL. I saw it at the MySQL site. > > The following is the URL: > http://www.mysql.com/articles/wizard/index.html > > Has anyone tried this on a PostgreSQL database ? No, not me. But as far as I can tell the SQL statements can quite easily be re-written in PostgreSQL: e. g. mysql> SELECT location, SUM(IF(gender='M',1,0)) AS M, SUM(IF(gender='F',1,0)) AS F -> FROM locations INNER JOIN employees USING (loc_code) GROUP BY location; becomes SELECT location, SUM(CASE WHEN gender='M' THEN 1 ELSE 0 END) AS "M", SUM(CASE WHEN gender='F' THEN 1 ELSE 0 END) AS "F", FROM locations LEFT JOIN employees ON (locations.loc_code=employees.loc_code) GROUP BY location; And this goes for the perl script as well. Regards, Christoph