Received: from beamish.nsd.ca (unknown [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id 8A027475AE5 for ; Fri, 12 Jul 2002 10:23:28 -0400 (EDT) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id KAA09212; Fri, 12 Jul 2002 10:23:28 -0400 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma009210; Fri, 12 Jul 02 10:23:27 -0400 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id KAA00762; Fri, 12 Jul 2002 10:23:27 -0400 Message-ID: <3D2EE781.597DB3FA@nsd.ca> Date: Fri, 12 Jul 2002 10:28:17 -0400 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: loyd@blackrobes.net Cc: pgsql-sql@postgresql.org Subject: Re: SQL problem with aggregate functions. References: <200207091036.17913.david.bouriaud@ac-rouen.fr> <200207110837.KAA07881@rodos> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200207/186 X-Sequence-Number: 8565 What is wrong with: select field_group, sum( case when f1 = 'D' then cnt else 0 end) as D_COUNT, sum( case when f1 = 'R' then cnt else 0 end) as R_COUNT, sum( case when f1 = 'X' then cnt else 0 end) as X_COUNT from (select field_group, f1, count (*) as cnt from tab group by field_group, f1) as ss group by field_group; It should be faster because there is less CASE evaluation. Loyd Goodbar wrote: > > I would suggest something like > > select sum(case when f1 = 'D' then 1 else 0 end) as D_COUNT, > sum(case when f1 = 'R' then 1 else 0 end) as R_COUNT, > sum(case when f1 = 'X' then 1 else 0 end) as X_COUNT > from tab > where f1 in ('D','R','X') > > Not sure what the "field group" represents. > > HTH, > Loyd > > On Thu, 11 Jul 2002 10:37:40 METDST, Christoph Haller wrote: > > >> > >> 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 > > > >---------------------------(end of broadcast)--------------------------- > >TIP 6: Have you searched our list archives? > > > >http://archives.postgresql.org > > -- > "Why, you can even hear yourself think." --Hobbes > "This is making me nervous. Let's go in." --Calvin > loyd@blackrobes.net ICQ#504581 http://www.blackrobes.net/ > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org