此查询可能出现问题:
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;
返回行结果,例如:
1
2
3
2
1
5
4
当我尝试获取完整计数时,指定在指定日期范围内购买的客户总数
_pj_cust_email_by_date
是一个视图,仅返回YYYY-MM-DD格式的电子邮件地址和订单日期。我无权使用除此视图之外的任何内容。
答案 0 :(得分:3)
GROUP BY
导致了这一点。
它会导致每个组返回一个结果行,对于每个不同的customer_email
值,都会返回此结果。
如果您需要不同电子邮件地址的总数,则需要删除GROUP BY
子句并将COUNT
更改为COUNT(DISTINCT customer_email)
。
答案 1 :(得分:2)
您需要进一步查询
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
这通常是方法,但由于您使用的是having count(order_date) > 0
,我认为您只需要
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 。