Re: count case when - PG 9.2

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Patrick B <patrickbakerbr(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: count case when - PG 9.2
Date: 2017-03-09 05:02:40
Message-ID: CAKFQuwa4i4bkG1retbTrUFOOZJQ8aHHWjfGqvMMZogWvPOsb4w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wednesday, March 8, 2017, Patrick B <patrickbakerbr(at)gmail(dot)com> wrote:

> Hi guys. How can I count using 'CASE WHEN'?
>
> Example:
>
> SELECT
>
> CASE
>
> WHEN AND c.regdate > EXTRACT(epoch FROM (now() - INTERVAL '14
> day'))
>
> THEN 'trial'
>
> WHEN last_pay > EXTRACT(epoch FROM (now() - INTERVAL '37 day'))
>
> THEN 'paying'
>
> END as account_status,
>
> c.id
>
> FROM public.clients c
>
> WHERE (
>
> (last_pay > EXTRACT('epoch' FROM now() - '12
> Months'::INTERVAL))
>
> )
>
> ORDER BY 1
>
> I wanna know how many of 'trial' and 'paying' customers the query returns.
> can you guys please advice how to do it?
>
> Thanks
> Patrick
>

Turn that into a subquery and group by...

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message John Iliffe 2017-03-09 05:09:00 Re: Unable to start postgresql
Previous Message Michael Paquier 2017-03-09 04:56:08 Re: Which release does a commit end up getting into?