From: Rod Taylor <rbt@rbt.ca>
To: jtx@hatesville.com
Cc: pgsql-sql@postgresql.org
Subject: Re: CASE
Date: 19 May 2003 19:24:49 -0400
Message-ID: <1053386689.24151.49.camel@jester> (raw)
In-Reply-To: <002201c31dd4$3f34c100$0000fea9@theplague>
References: <002201c31dd4$3f34c100$0000fea9@theplague>
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:
>
> select sum(numbers) from lists where uid=1;
>
> It's possible that there may be 0 rows where uid == 1. Instead of
> returning 1 blank row, I want it to return '0'. So, I tried this:
>
> 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 = 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 = 1
) as tab;
--
Rod Taylor <rbt@rbt.ca>
PGP Key: http://www.rbt.ca/rbtpub.ascAttachments:
signature.asc (application/pgp-signature, 186B) download
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: rbt@rbt.ca, jtx@hatesville.com
Subject: Re: CASE
In-Reply-To: <1053386689.24151.49.camel@jester>
* 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