首页 > 解决方案 > 所有属性的 null 和 not null 的分析值计数

问题描述

我有一个表,它在下表中具有例如 3 个属性,使用 hive / sql 我有一个用例,其中包含 50 多个属性,这些属性要求我根据当前使用对这些值 nullcount、notnull 等进行分析 -情况下,我无法使用 pyspark/plsql 进行任何数据转换,这遇到了障碍

出于演示目的,假设我有 3 个属性,只需要 2 个表的 notnull 计数,马来西亚和泰国

describe malaysia

ColumnName |  Datatype
custid     |  String
productid  |  String
addressid  |  String

describe thailand

ColumnName |  Datatype
custid     |  String
productid  |  String
addressid  |  String

按照我目前的方法,我就是这样做的,

select COUNT(CASE WHEN custid IS NOT NULL then 1 ELSE NULL END) as "NullCount",
COUNT(CASE WHEN productid IS NOT NULL then 1 ELSE NULL END) as "NullCount",
COUNT(CASE WHEN addressid IS NOT NULL then 1 ELSE NULL END) as "NullCount" 
from malaysia 
union
select COUNT(CASE WHEN custid IS NOT NULL then 1 ELSE NULL END) as "NullCount",
COUNT(CASE WHEN productid IS NOT NULL then 1 ELSE NULL END) as "NullCount",
COUNT(CASE WHEN addressid IS NOT NULL then 1 ELSE NULL END) as "NullCount" 
from thailand

知道我每个表有超过 50 个属性,查询会很长,我觉得它不是正确的方法,因为维护会变得混乱和困难。我想知道是否有更好的方法来产生相同的结果。

标签: sqlhivebigdata

解决方案


修复你的数据模型!您应该将所有数据放在一个中,其中有一列 for country。然后你可以简单地做:

select country,
       count(custid) as custid_not_null,
       count(productid) as productid_not_null,
       count(addressid) as addressid_not_null
from all_countries
group by country ;

请注意,count(<expression>)计算表达式的非NULL值的数量。case表达方式完全是多余的。

如果没有,您可以创建一个视图:

create view all_counties as
    select 'malaysia' as country, t.*
    from malaysia m
    union all
    select 'thailand' as country, t.*
    from thailand t
    union all
    . . .;

但我建议您将所有数据放在一个表中。


推荐阅读