Re: multiple counts using CASE - Mailing list pgsql-general

From Tom Lane
Subject Re: multiple counts using CASE
Date
Msg-id 29058.1016551112@sss.pgh.pa.us
Whole thread Raw
In response to multiple counts using CASE  ("Johnson, Shaunn" <SJohnson6@bcbsm.com>)
List pgsql-general
"Johnson, Shaunn" <SJohnson6@bcbsm.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

pgsql-general by date:

Previous
From: "Johnson, Shaunn"
Date:
Subject: multiple counts using CASE
Next
From: Andrew Sullivan
Date:
Subject: Re: locking problems