Re: multiple counts using CASE

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Johnson, Shaunn" <SJohnson6(at)bcbsm(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: multiple counts using CASE
Date: 2002-03-19 15:18:32
Message-ID: 29058.1016551112@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Johnson, Shaunn" <SJohnson6(at)bcbsm(dot)com> writes:
> Wonder if it is possible to get multiple counts
> from ONE query using the CASE statement.

If I'm understanding you correctly, you can't do what you want with
a single aggregate function call, because that will only give you
one result.

Consider something like

select
sum(case when logic = '1' then 1 else 0 end),
sum(case when logic = '2' then 1 else 0 end),
sum(case when logic = '3' then 1 else 0 end)
from ...

I also wonder whether you aren't reinventing the notion of GROUP BY.
F'r instance

select logic, count(*) from ... group by logic;

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Sullivan 2002-03-19 15:28:42 Re: locking problems
Previous Message Eric Lee Green 2002-03-19 15:08:54 Re: Performance issues