Re: CASE

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: James Taylor <jtx(at)hatesville(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: CASE
Date: 2003-05-19 22:49:21
Message-ID: 20030519154658.Q43251-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Sun, 18 May 2003, 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;

I believe the case would be:

case when sum(numbers) is null then 0 else sum(numbers) end

It's more like the ternary operator than a switch statement.

In response to

  • CASE at 2003-05-19 06:59:46 from James Taylor

Browse pgsql-sql by date

  From Date Subject
Next Message David W Noon 2003-05-19 23:19:32 Re: Performance on temp table inserts
Previous Message Stephan Szabo 2003-05-19 22:28:06 Re: "deadlock detected" / cascading locks