Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?

From: "Jim Buttafuoco" <jim(at)contactbda(dot)com>
To: "'Bryce Nesbitt'" <bryce1(at)obviously(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
Date: 2007-05-14 23:16:14
Message-ID: 200705142316.l4ENGGS4010065@contactbda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Use case statement and sum to get a count where status=20...

For example

Select sum(case when status=20 then 1 else 0 end) as status20,
Sum(case when status=30 then 1 else 0 end) as status30
....

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
On Behalf Of Bryce Nesbitt
Sent: Monday, May 14, 2007 6:56 PM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] Doing a conditional aggregate (e.g. count(*) if x=y) in
postgres?

All;
Is there a way to get a conditional aggregate? I have this two column view:

SELECT count(*) AS count, xx_plan.plan_name
FROM xx_membership
JOIN xx_account USING (account_id)
JOIN xx_plan USING (plan_id)
WHERE xx_membership.status = 10
GROUP BY xx_plan.plan_name;

And would like to add additional columns (not rows) breaking out
"status=20" and "status=30" totals.
Is this possible without a stored procedure?

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Geoff Tolley 2007-05-14 23:18:39 Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
Previous Message Paul Lambert 2007-05-14 22:56:19 Re: pg_dump?