MySQL query count () returns rows instead of general?

What could be wrong with this query:

select count(customer_email) as num_prev
  from _pj_cust_email_by_date
  where order_date < '2011-02'
  and customer_email is not null
  group by customer_email having count(order_date) > 0;

Returns string results, such as:

1
2
3
2
1
5
4

When I try to get a complete list of how many customers I’ve purchased in total over the specified date range?

_pj_cust_email_by_date- This is a view that returns only the email address and order date in the format YYYY-MM-DD. I do not have access to use anything other than this.

+3
source share
2 answers

You need to query him further

select count(*) CustomerCount
from (
    select count(customer_email) as num_prev
    from _pj_cust_email_by_date
    where order_date < '2011-02'
    and customer_email is not null
    group by customer_email having count(order_date) > 0;
) as innercount

This is usually an approach, but since you are using having count(order_date) > 0, it seems to me you only need

select count(distinct customer_email) as num_prev
from _pj_cust_email_by_date
where order_date < '2011-02' and customer_email is not null

HAVING order_dates, HAVING dud.

+2

GROUP BY .

, customer_email.

, GROUP BY COUNT COUNT(DISTINCT customer_email).

+3

Source: https://habr.com/ru/post/1794005/


All Articles