From: Hubert depesz Lubaczewski <depesz@depesz.pl>
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL problem with aggregate functions.
Date: Mon, 22 Jul 2002 10:26:44 +0200
Message-ID: <20020722082644.GA32346@depesz.pl> (raw)
In-Reply-To: <200207091036.17913.david.bouriaud@ac-rouen.fr>
References: <200207091036.17913.david.bouriaud@ac-rouen.fr>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: depesz@depesz.pl, david.bouriaud@ac-rouen.fr
Subject: Re: SQL problem with aggregate functions.
In-Reply-To: <20020722082644.GA32346@depesz.pl>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox