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

MYSQL Query calculate percentage of emails not null

我有这两个问题。

计算空电子邮件数

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

计算每封电子邮件的数量

SELECT COUNT(distinct c_number) as total FROM distinct_customers;

我正在尝试将这些组合到查询中,以便它为我提供一定比例的有效电子邮件(非空)

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

数学上应该是

643(空邮件)*100 / 1292(邮件总数)

我认为最简单的方法是使用 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