Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id E22659FB830 for ; Mon, 14 May 2007 20:16:29 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 28906-09 for ; Mon, 14 May 2007 20:16:24 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 1C5F89FB5C8 for ; Mon, 14 May 2007 20:16:25 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from contactbda.com (ipn36372-e65122.cidr.lightship.net [216.204.66.226]) by svr4.postgresql.org (Postfix) with ESMTP id 0E8195AF031 for ; Mon, 14 May 2007 23:16:23 +0000 (GMT) Received: from JIMSDELL2 (pool-70-20-57-125.man.east.verizon.net [70.20.57.125]) (authenticated bits=0) by contactbda.com (8.13.8/8.13.8/Debian-3) with ESMTP id l4ENGGS4010065; Mon, 14 May 2007 19:16:20 -0400 Message-Id: <200705142316.l4ENGGS4010065@contactbda.com> From: "Jim Buttafuoco" To: "'Bryce Nesbitt'" , Subject: Re: Doing a conditional aggregate (e.g. count(*) if x=y) in postgres? Date: Mon, 14 May 2007 19:16:14 -0400 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 In-Reply-To: <4648E8F1.4020609@obviously.com> thread-index: AceWe0z6uOhLGWFbR+uOiaw4T6F+tQAAlgWw X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200705/113 X-Sequence-Number: 28393 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