Received: from mgc.pl-1to1-Magic (neron.mgc.pl [62.181.184.146]) by postgresql.org (Postfix) with SMTP id 8CBAA474E4E for ; Mon, 22 Jul 2002 04:26:45 -0400 (EDT) Received: (qmail 503 invoked by uid 500); 22 Jul 2002 08:26:44 -0000 Date: Mon, 22 Jul 2002 10:26:44 +0200 From: Hubert depesz Lubaczewski To: David BOURIAUD Cc: pgsql-sql@postgresql.org Subject: Re: SQL problem with aggregate functions. Message-ID: <20020722082644.GA32346@depesz.pl> Reply-To: depesz@depesz.pl References: <200207091036.17913.david.bouriaud@ac-rouen.fr> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="bg08WKrSYDhXBjb5" Content-Disposition: inline In-Reply-To: <200207091036.17913.david.bouriaud@ac-rouen.fr> User-Agent: Mutt/1.4i X-info-en-1: this message *may* reflect my personal opinion. it is *not* X-info-en-2: intended to reflect those of my employer, or anyone else. X-info-pl-1: wszelkie opinie =?iso-8859-2?Q?wyra=BFone_?= =?iso-8859-2?Q?w_tym_li=B6cie_prezentuj=B1_wy=B3=B1cznie_pogl=B1dy?= X-info-pl-2: autora listu. opinie =?iso-8859-2?Q?te_?= =?iso-8859-2?Q?w_=BFadnym__razie_nie__wyra=BFaj=B1__pogl=B1d=F3w?= X-info-pl-3: pracodawcy autora =?iso-8859-2?Q?listu_?= =?iso-8859-2?Q?_ani__innych__zwi=B1zanych__z__nim__os=F3b=2E?= X-Operating-System: Linux 2.4.18 i686 Pentium_III_(Coppermine) X-code: perl -e 's&&_b`s .oqnb.lntmsr_&&&y&, -{&--|&&&s&&$_&ee&&map{y&--|&, -{&&&s&&_gd`c , b14///// .cdu.tq`mcnl=${_}_&&&y&, -{&--|&&&s ee$_eee}grep m m[hs]dwm&&!$x{$_}++, map m msm&&$`, split v10' X-Archive-Number: 200207/320 X-Sequence-Number: 8698 --bg08WKrSYDhXBjb5 Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 =3D t1.field_group and= t2.field=3D'D') as count_of_d, (select count(*) from table t2 where t2.field_group =3D t1.field_group and= t2.field=3D'R') as count_of_r, (select count(*) from table t2 where t2.field_group =3D t1.field_group and= t2.field=3D'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 --=20 hubert depesz lubaczewski http://www.depesz.pl/ ------------------------------------------------------------------------ M=F3j Bo=BFe, spraw abym milcza=B3, dop=F3ki si=EA nie upewni=EA, =BFe na= prawd=EA mam co=B6 do powiedzenia. (c) 1998 depesz --bg08WKrSYDhXBjb5 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9O8HEKBwKA0prOAkRAlroAJ9mRetQzq/anxtYUOkm037253L6ygCbBM7/ qJ9Nv+8v0+ujph87r6H2ne8= =dRwz -----END PGP SIGNATURE----- --bg08WKrSYDhXBjb5--