X-Original-To: pgsql-sql@postgresql.org Received: from localhost (unknown [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id AC40C9253E7 for ; Mon, 19 May 2003 21:05:55 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193:10024]) (amavisd-new) with ESMTP id 13666-03 for ; Mon, 19 May 2003 21:05:52 -0400 (EDT) Received: from tomts7-srv.bellnexxia.net (tomts7.bellnexxia.net [209.226.175.40]) by developer.postgresql.org (Postfix) with ESMTP id E667B924FFE for ; Mon, 19 May 2003 19:24:46 -0400 (EDT) Received: from [192.168.1.200] ([64.231.213.3]) by tomts7-srv.bellnexxia.net (InterMail vM.5.01.05.32 201-253-122-126-132-20030307) with ESMTP id <20030519232445.PAQJ23765.tomts7-srv.bellnexxia.net@[192.168.1.200]>; Mon, 19 May 2003 19:24:45 -0400 Subject: Re: CASE From: Rod Taylor To: jtx@hatesville.com Cc: pgsql-sql@postgresql.org In-Reply-To: <002201c31dd4$3f34c100$0000fea9@theplague> References: <002201c31dd4$3f34c100$0000fea9@theplague> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-x6SPYLWv4yFJUehA1nSZ" Organization: Message-Id: <1053386689.24151.49.camel@jester> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.4 Date: 19 May 2003 19:24:49 -0400 X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/374 X-Sequence-Number: 13539 --=-x6SPYLWv4yFJUehA1nSZ Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Mon, 2003-05-19 at 02:59, James Taylor wrote: > Hi everyone, I'm trying to figure out how to get CASE working in a sum > aggregate function. Pretty simple query: >=20 > select sum(numbers) from lists where uid=3D1; >=20 > It's possible that there may be 0 rows where uid =3D=3D 1. Instead of > returning 1 blank row, I want it to return '0'. So, I tried this: >=20 > select case(sum(numbers)) when null then 0 else sum(numbers) end from > list_results; Use Coalesce: SELECT coalesce(sum(numbers), 0) FROM lists WHERE uid =3D 1; To use case, efficiently, if you really want: SELECT CASE WHEN sum_numbers IS NULL THEN 0 ELSE sum_numbers END AS sum_numbers FROM (SELECT sum(numbers) AS sum_numbers FROM lists WHERE uid =3D 1 ) as tab; --=20 Rod Taylor PGP Key: http://www.rbt.ca/rbtpub.asc --=-x6SPYLWv4yFJUehA1nSZ Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQA+yWfA6DETLow6vwwRAht7AJ9N7kFauyQMt1DvB4uLLfX8hhHknQCfamQX QqjfSsv8KMF2qr6ubKsvJxM= =H6tI -----END PGP SIGNATURE----- --=-x6SPYLWv4yFJUehA1nSZ--