Re: SQL problem with aggregate functions.

From: Hubert depesz Lubaczewski <depesz(at)depesz(dot)pl>
To: David BOURIAUD <david(dot)bouriaud(at)ac-rouen(dot)fr>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL problem with aggregate functions.
Date: 2002-07-22 08:26:44
Message-ID: 20020722082644.GA32346@depesz.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Jul 09, 2002 at 10:36:17AM +0200, David BOURIAUD wrote:
> Field group | count of D | count of R | count of X.

if you want this that way, i suggest using subselects.
like:
select
distinct field_group,
(select count(*) from table t2 where t2.field_group = t1.field_group and t2.field='D') as count_of_d,
(select count(*) from table t2 where t2.field_group = t1.field_group and t2.field='R') as count_of_r,
(select count(*) from table t2 where t2.field_group = t1.field_group and t2.field='X') as count_of_x
from
table;

should work the way you want it.

anyway, i belive that making this:

select field_group, field, count(*) from table where field in
('D','R','X') group by field_group, field;

and then processing results in client application, should be a little
bit better/faster solution.

depesz

--
hubert depesz lubaczewski http://www.depesz.pl/
------------------------------------------------------------------------
Mój Boże, spraw abym milczał, dopóki się nie upewnię, że naprawdę mam
coś do powiedzenia. (c) 1998 depesz

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Elielson Fontanezi 2002-07-22 12:36:54 Editor for pgsql
Previous Message mallah 2002-07-22 03:26:25 Re: 'no wait' option for locks