Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id DF781475A20 for ; Thu, 11 Jul 2002 04:37:41 -0400 (EDT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Thu Jul 11 10:37:41 2002 +0200 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id KAA05256; Thu, 11 Jul 2002 10:37:40 +0200 (METDST) From: Christoph Haller Message-Id: <200207110837.KAA07881@rodos> Subject: Re: SQL problem with aggregate functions. To: david.bouriaud@ac-rouen.fr (David BOURIAUD) Date: Thu, 11 Jul 2002 10:37:40 METDST Cc: pgsql-sql@postgresql.org In-Reply-To: <200207091036.17913.david.bouriaud@ac-rouen.fr>; from "David BOURIAUD" at Jul 9, 102 2:50 pm X-Mailer: Elm [revision: 212.4] X-Archive-Number: 200207/154 X-Sequence-Number: 8533 > > I've got a table in which there is a field that can have one amongst 3 > possible values : D, R, X. Is it possible to get in one query the count of > this different values.Please, note that I don't want to have a querry like > this : > "select count (*) from tab group by f1;", cause i want to get all the possible > count values in one row (these data are already grouped on another field). > To give a more accurate example, here is what I want to retrieve : > > Field group | count of D | count of R | count of X. > > Any clues ? > -- What about something like SELECT SUM(f1_d) AS count_d, SUM(f1_r) AS count_r, SUM(f1_x) AS count_x FROM ( SELECT CASE WHEN f1 = 'D' THEN 1 ELSE 0 END AS f1_d, CASE WHEN f1 = 'R' THEN 1 ELSE 0 END AS f1_r, CASE WHEN f1 = 'X' THEN 1 ELSE 0 END AS f1_x FROM tab ) AS foo ; Regards, Christoph