我有一个像
这样的表store_code | country_code | 时间戳 | 空 | FR | 1234567890 |
---|---|---|
123 | FR | 1234567890 |
456 | GB | 1234567890 |
789 | GB | 1234567890 |
一种方法只使用avg()
:
SELECT country_code,
AVG(case when store_code IS NULL then 1.0 else 0 end) as null_ratio
FROM table
GROUP BY country_code;
如果要计数并除以总数,则使用COUNTIF()
:
SELECT country_code,
COUNTIF(store_code IS NULL) * 1.0 / COUNT(*) as null_ratio
FROM table
GROUP BY country_code;