pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedDoing a conditional aggregate (e.g. count(*) if x=y) in postgres?
4+ messages / 3 participants
[nested] [flat]
* Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
@ 2007-05-14 22:55 Bryce Nesbitt <bryce1@obviously.com>
2007-05-14 23:16 ` Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Jim Buttafuoco <jim@contactbda.com>
2007-05-14 23:18 ` Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Geoff Tolley <geoff@polimetrix.com>
0 siblings, 2 replies; 4+ messages in thread
From: Bryce Nesbitt @ 2007-05-14 22:55 UTC (permalink / raw)
To: pgsql-sql
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?
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
2007-05-14 22:55 Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Bryce Nesbitt <bryce1@obviously.com>
@ 2007-05-14 23:16 ` Jim Buttafuoco <jim@contactbda.com>
1 sibling, 0 replies; 4+ messages in thread
From: Jim Buttafuoco @ 2007-05-14 23:16 UTC (permalink / raw)
To: 'Bryce Nesbitt' <bryce1@obviously.com>; 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@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Bryce Nesbitt
Sent: Monday, May 14, 2007 6:56 PM
To: pgsql-sql@postgresql.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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
2007-05-14 22:55 Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Bryce Nesbitt <bryce1@obviously.com>
@ 2007-05-14 23:18 ` Geoff Tolley <geoff@polimetrix.com>
2007-05-14 23:48 ` Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Bryce Nesbitt <bryce1@obviously.com>
1 sibling, 1 reply; 4+ messages in thread
From: Geoff Tolley @ 2007-05-14 23:18 UTC (permalink / raw)
To: Bryce Nesbitt <bryce1@obviously.com>; +Cc: pgsql-sql
Bryce Nesbitt wrote:
> 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?
SELECT sum(CASE WHEN xx_membership.status = 10 THEN 1 ELSE 0 END) AS sum10,
sum(CASE WHEN xx_membership.status = 20 THEN 1 ELSE 0 END) AS sum20,
sum(CASE WHEN xx_membership.status = 30 THEN 1 ELSE 0 END) AS sum30,
xx_plan.plan_name
FROM xx_membership
JOIN xx_account USING (account_id)
JOIN xx_plan USING (plan_id)
WHERE xx_membership.status IN (10,20,30)
GROUP BY xx_plan.plan_name;
You may or may not care about including the WHERE clause there depending
upon its selectivity and whether there's an index for the planner to use.
HTH,
Geoff
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
2007-05-14 22:55 Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Bryce Nesbitt <bryce1@obviously.com>
2007-05-14 23:18 ` Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Geoff Tolley <geoff@polimetrix.com>
@ 2007-05-14 23:48 ` Bryce Nesbitt <bryce1@obviously.com>
0 siblings, 0 replies; 4+ messages in thread
From: Bryce Nesbitt @ 2007-05-14 23:48 UTC (permalink / raw)
To: pgsql-sql
Ah perfect. I was struggling with CASE outside the SUM(), which was not
working.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2007-05-14 23:48 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-05-14 22:55 Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Bryce Nesbitt <bryce1@obviously.com>
2007-05-14 23:16 ` Jim Buttafuoco <jim@contactbda.com>
2007-05-14 23:18 ` Geoff Tolley <geoff@polimetrix.com>
2007-05-14 23:48 ` Bryce Nesbitt <bryce1@obviously.com>
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