【问题标题】:Profiling values for null and not null count on all attributes所有属性的 null 和 not null 的分析值计数
【发布时间】:2021-04-07 02:32:57
【问题描述】:

我有一个表,它使用 hive / sql 在下表中具有例如 3 个属性 我有一个用例,其中包含 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 个属性,查询会很长,我觉得它不是正确的方法,因为维护会变得混乱和困难。我想知道是否有更好的方法来产生相同的结果。

【问题讨论】:

  • 修复你的数据模型!这是根本问题。您不应该将数据存储在多个表中,就像在同一个表中存储多行一样。

标签: sql hive bigdata


【解决方案1】:

修复您的数据模型!您应该将所有数据放在一个单个 表中,其中有一列是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
    . . .;

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

【讨论】:

  • 我已经对我的数据进行了重组。但是,我在一个按国家/地区分区的表中有 100 多个属性,但我可以使用 group by 子句。我想知道是否有任何语句可以让我选择所有属性作为列而不是查询' select as_of_date as columnname ... union select currency as colummane 等。
  • 拥有 100 个属性,而不是像您给出的示例那样一一指定,count(custid) as custidnotnull.. count product id as productnotnull...,我们是否可以在一行中查询为所有属性提供空记录?
  • @FAves 。 . .将所有数据放在一张表中!修复数据模型。
【解决方案2】:

您需要稍微更改一下您的 sql。而不是计数使用总和。我添加了总数以便更好地分析。我没有任何简单的方法,但您可以将列列表放入 XL 中并从中创建一个公式,该公式将自动创建此字符串 SUM(CASE WHEN custid IS NOT NULL then 1 ELSE 0 END) as "Nullcustid"

select 
COUNT(*) "TotalCount",
SUM(CASE WHEN custid IS NOT NULL then 1 ELSE 0 END) as "NullCust",
SUM(CASE WHEN productid IS NOT NULL then 1 ELSE  0 END) as "Nullprod",
SUM(CASE WHEN addressid IS NOT NULL then 1 ELSE 0 END) as "Nulladdress" 
from malaysia 
union
select 
COUNT(*) "TotalCount",
SUM(CASE WHEN custid IS NOT NULL then 1 ELSE 0 END) as "NullCust",
SUM(CASE WHEN productid IS NOT NULL then 1 ELSE  0 END) as "Nullprod",
SUM(CASE WHEN addressid IS NOT NULL then 1 ELSE 0 END) as "Nulladdress" 
from thailand

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多