MYSQL查询计算电子邮件百分比不为null

我有这两个查询。

计算为空的电子邮件计数

SELECT COUNT(*) as invalid_email FROM distinct_customers WHERE c_email IS NULL;

计算每封电子邮件的数量

SELECT COUNT(distinct c_number) as total FROM distinct_customers;

我正在尝试将这些内容与查询结合起来,以便为我提供一定百分比的有效电子邮件(不为null)

我尝试了几种方法,但我不是mysql专家。

从数学上讲应该是

643(空电子邮件)* 100/1292(总电子邮件)

nxh123 回答:MYSQL查询计算电子邮件百分比不为null

我认为最简单的方法是使用AVG() -假设您希望按行:

SELECT AVG(c_email IS NOT NULL) as invalid_email_ratio,100 * AVG(c_email IS NOT NULL) as invalid_email_percentile           
FROM distinct_customers ;
,

您可以在下面尝试-

SELECT (COUNT(case when c_email IS NULL then 1 end)*100.00)/
        COUNT(distinct c_number) as percentage
FROM distinct_customers
本文链接:https://www.f2er.com/3029349.html

大家都在问