【问题标题】:I need to get the total number of Null values from multiple columns of a table [closed]我需要从表的多个列中获取 Null 值的总数 [关闭]
【发布时间】:2018-10-26 08:58:56
【问题描述】:

示例表:

color|country|value1|value2|value3
-----------------------------------
Red  | India |1     |null  |4
Blue | USA   |4     |2     |null
Red  | USA   |null  |1     |2
Blue | null  |4     |1     |1

输出:

Target | No_1 | No_2 | No_4 | No_null
value1 | 1    | 0    | 2    | 1
value2 | 2    | 1    | 0    | 1
value3 | 1    | 1    | 1    | 1

【问题讨论】:

  • 你尝试了什么?
  • 什么 rdbms? sql只是语言..
  • 我知道用 SQL 编写它的方法的想法。请指教..
  • 在一个聚合函数中你可以有一个 SUM 并且在参数中做空检查。类似于SUM(case condition_col1 end + case condition_col1 end)
  • sql条件SUM有already been answered多次,检查help center

标签: sql pyspark


【解决方案1】:

您可以尝试使用UNION ALL 然后CASE WHEN 进行条件聚合

select 
    target, 
    count(case when val=1 then 1 end) as no_1,
    count(case when val=2 then 1 end) as no_2,
    count(case when val=4 then 1 end) as no_4,
    count(case when val is null then 1 end) as no_null
from
(
   select 'value1' as target,value1 as val from tablename
   union all
   select 'value2',value2 from tablename
   union all
   select 'value3', value3 from tablename
)X group by target

【讨论】:

  • 所有数据都在同一张表中,不需要unpivot而是unpivot
  • @bradbury9,据我了解,联合所有都需要,因为值在单独的列中
  • 我同意,我很困惑
猜你喜欢
  • 1970-01-01
  • 2013-04-30
  • 2023-03-28
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 2019-12-09
相关资源
最近更新 更多