sql - 所有属性的 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 个属性,查询会很长,我觉得它不是正确的方法,因为维护会变得混乱和困难。我想知道是否有更好的方法来产生相同的结果。
解决方案
修复你的数据模型!您应该将所有数据放在一个表中,其中有一列 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
. . .;
但我建议您将所有数据放在一个表中。
推荐阅读
- python - 如何在 QVboxLayout 中的小部件之间添加自定义空间?
- python - 我应该调整 REST API 请求验证和数据库错误响应的格式吗?
- hash - 为什么 md5(和其他哈希算法)不在 base32 中输出?
- python - 尝试在 Python 中编写长度和宽度代码时“解压的值太多”
- lotus-domino - LotusScript - HCL Domino 11 服务器上的 MS 文字处理问题
- javascript - Uncaught TypeError: Cannot read property 'style' of undefined - 我在 body 属性的底部添加了脚本
- javascript - 如何使用 Input + Button + JS 脚本和 Mailchimp 发送电子邮件
- javascript - 将 dataTable 输入字段宽度设置为自动
- android - 使用函数引用来注册和注销监听器是否安全?
- reactjs - 样式化组件 - 具有嵌套类的 css 变量