In the below query, I get the count of customers who were active between "2017-09-01 00:00:00" and "2017-11-31 23:59:59" as cust_90 and would like to add another column to find the count of customers who were active between "2017-11-01 00:00:00" and "2017-11-31 23:59:59" (a subset of the whole period).
select custid, count(distinct concat(visit1, visit2)) as cust_90
from test1
where date_time between "2017-09-01 00:00:00" and "2017-11-31 23:59:59"
and custid = '234214124'
group by custid;
Sample output:
CustomerName cust_90 cust_30
David 38 15
Wondering whether I could have a subquery in the above query to find the customers active in a month. Any suggestions would be great.
This is called conditional aggregation which can be done using a case
expression.
select custid,
count(distinct concat(visit1, visit2) end) as cust_90,
count(distinct case when to_date(date_time)>='2017-11-01' then concat(visit1, visit2) end) as cust_30
from test1
where date_time >= '2017-09-01' and date_time < '2017-12-01'
and custid = '234214124'
group by custid;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.