【问题标题】:SQL: Percentage Missing and Unique Counts of Table ColumnsSQL:表列的缺失百分比和唯一计数
【发布时间】:2021-04-29 19:28:54
【问题描述】:
假设我有一个有 4 列的表格。
我想知道的每一列:
如果我有一个包含 A B C 和 D 列的表,
例如,上述情况的预期结果是:
Column_Name | PctMissing | UniqueCount
A | 0.15 | 16
B | 0 | 320
C | 0.3 | 190
D | 0.05 | 8
【问题讨论】:
标签:
sql
pivot
unique
missing-data
calculation
【解决方案1】:
如果您知道列数,我可能只使用union all:
select 'a' as Column_Name,
1.0*count(case when a is null then 1 end)/count(*) as PctMissing,
count(distinct a) as UniqueCount
from t
union all
select 'b' as Column_Name,
1.0*count(case when b is null then 1 end)/count(*) as PctMissing,
count(distinct b) as UniqueCount
from t
union all
select 'c' as Column_Name,
1.0*count(case when c is null then 1 end)/count(*) as PctMissing,
count(distinct c) as UniqueCount
from t
union all
select 'd' as Column_Name,
1.0*count(case when d is null then 1 end)/count(*) as PctMissing,
count(distinct d) as UniqueCount
from t
Fiddle Demo
根据您的数据库,还有其他方法,但可能比union all 更令人困惑。
【解决方案2】:
我会这样写:
select 'a' as column_name,
avg(case when a is null then 1.0 else 0 end) as missing_ratio,
count(distinct a) as unique_count
from t
union all
select 'b' as column_name,
avg(case when b is null then 1.0 else 0 end) as missing_ratio,
count(distinct b) as unique_count
from t
union all
select 'c' as column_name,
avg(case when c is null then 1.0 else 0 end) as missing_ratio,
count(distinct c) as unique_count
from t
union all
select 'd' as column_name,
avg(case when d is null then 1.0 else 0 end) as missing_ratio,
count(distinct d) as unique_count
from t;