pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jim Buttafuoco <jim@contactbda.com>
To: 'Bryce Nesbitt' <bryce1@obviously.com>
To: pgsql-sql@postgresql.org
Subject: Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
Date: Mon, 14 May 2007 19:16:14 -0400
Message-ID: <200705142316.l4ENGGS4010065@contactbda.com> (raw)
In-Reply-To: <4648E8F1.4020609@obviously.com>

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





view thread (4+ messages)  latest in thread

Message-ID: <200705142316.l4ENGGS4010065@contactbda.com>
Permalink:  ../200705142316.l4ENGGS4010065@contactbda.com/
Also on:    postgresql.org/message-id/200705142316.l4ENGGS4010065@contactbda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jim@contactbda.com, bryce1@obviously.com
  Subject: Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres?
  In-Reply-To: <200705142316.l4ENGGS4010065@contactbda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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