Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:表列的丢失和唯一计数百分比_Sql_Pivot_Unique_Missing Data_Calculation - Fatal编程技术网

SQL:表列的丢失和唯一计数百分比

SQL:表列的丢失和唯一计数百分比,sql,pivot,unique,missing-data,calculation,Sql,Pivot,Unique,Missing Data,Calculation,假设我有一个4列的表。 我想知道的每个专栏的: 缺少值的百分比(空计数)和 唯一计数 如果我有一个列为a、B、C和D的表, 上述情况的预期结果将是,例如: Column_Name | PctMissing | UniqueCount A | 0.15 | 16 B | 0 | 320 C | 0.3 | 190 D | 0.05 | 8 如果您知道列的数量

假设我有一个4列的表。 我想知道的每个专栏的:

  • 缺少值的百分比(空计数)和
  • 唯一计数
如果我有一个列为a、B、C和D的表, 上述情况的预期结果将是,例如:

Column_Name | PctMissing | UniqueCount
A           | 0.15       | 16
B           | 0          | 320
C           | 0.3        | 190
D           | 0.05       | 8

如果您知道列的数量,我可能会使用
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

根据您的数据库,还有其他方法,但可能比union all更令人困惑

我将这样写:

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;

用你正在使用的数据库标记你的问题。谢谢戈登!确实帮了我很多。。。