count case when - PG 9.2

Started by Patrick Babout 9 years ago7 messagesgeneral
Jump to latest
#1Patrick B
patrickbakerbr@gmail.com

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

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Patrick B (#1)
Re: count case when - PG 9.2

On Wednesday, March 8, 2017, Patrick B <patrickbakerbr@gmail.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.

#3vinny
vinny@xs4all.nl
In reply to: Patrick B (#1)
Re: count case when - PG 9.2

On 2017-03-09 05:27, Patrick B 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 [1]

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

comparisons like "A>B" return a boolean. Booleans can be cast to
integers, and integers can be summed.

SUM((A>B)::int)

But depending on the situation, indexes etc it could be faster to run e
separate count query, you'll have to test that.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Patrick B
patrickbakerbr@gmail.com
In reply to: vinny (#3)
Re: count case when - PG 9.2

2017-03-09 23:15 GMT+13:00 vinny <vinny@xs4all.nl>:

On 2017-03-09 05:27, Patrick B 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 [1]

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

comparisons like "A>B" return a boolean. Booleans can be cast to integers,
and integers can be summed.

SUM((A>B)::int)

But depending on the situation, indexes etc it could be faster to run e
separate count query, you'll have to test that.

Could you please guys give me a query as an example?

Thanks
P.

#5David G. Johnston
david.g.johnston@gmail.com
In reply to: Patrick B (#4)
Re: count case when - PG 9.2

On Thu, Mar 9, 2017 at 2:01 PM, Patrick B <patrickbakerbr@gmail.com> wrote:

Could you please guys give me a query as an example?

​Maybe if you describe exactly how you want the output to appear. And
maybe tee things up by writing a query that gets close and with some
example data. Something that can be copy-paste-executed

​David J.

#6Yasin Sari
yasinsari81@googlemail.com
In reply to: Patrick B (#4)
Re: count case when - PG 9.2

if you want see account_status and the count()- try this:

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 <http://c.id/&gt;ount(*)

FROM public.clients c

WHERE (

(last_pay > EXTRACT('epoch' FROM now() - '12
Months'::INTERVAL))

)

group by 1

ORDER BY 1

10 Mar 2017 Cum, 00:02 tarihinde, Patrick B <patrickbakerbr@gmail.com> şunu
yazdı:

Show quoted text

2017-03-09 23:15 GMT+13:00 vinny <vinny@xs4all.nl>:

On 2017-03-09 05:27, Patrick B 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 [1]

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

comparisons like "A>B" return a boolean. Booleans can be cast to integers,
and integers can be summed.

SUM((A>B)::int)

But depending on the situation, indexes etc it could be faster to run e
separate count query, you'll have to test that.

Could you please guys give me a query as an example?

Thanks
P.

#7Patrick B
patrickbakerbr@gmail.com
In reply to: Yasin Sari (#6)
Re: count case when - PG 9.2

2017-03-10 10:17 GMT+13:00 Yasin Sari <yasinsari81@googlemail.com>:

if you want see account_status and the count()- try this:

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 <http://c.id/&gt;ount(*)

FROM public.clients c

WHERE (

(last_pay > EXTRACT('epoch' FROM now() - '12
Months'::INTERVAL))

)

group by 1

ORDER BY 1

Thanks Yasin! That worked.

P.