【问题标题】:Pyspark subquery on the same table同一张表上的 Pyspark 子查询
【发布时间】:2022-12-07 07:03:02
【问题描述】:

我有一些数据有年、月、日、column_x。 column_x 可以缺失或不缺失。我要生成的是 column_x 的缺失率。为此,我尝试创建两列,其中包含总行号,即 total_count,以及代表 column_x == null 的计数列。

我正在尝试创建如下所示的内容:

total_count | count | year | month | date
60          | 20    | 2022 | 12    | 01

所以我以后可以做count / total_count来获得一些百分比。 但是,我不确定如何生成查询。

我尝试了子查询,但它抛出了一个错误。我如何通过 pyspark 或 sql 子查询实现这一点? (我也可以注册临时表并运行 sql 查询)

【问题讨论】:

    标签: mysql sql pyspark


    【解决方案1】:

    您可以进行条件计数。在 MySQL 中:

    select year, month, day, 
        count(*) as cnt_total,
        count(column_x) as cnt_x_not_null,
        sum(column_x is null) as cnt_x_null,
        avg(column_x is null) as ratio_x_null 
    from mytable
    group by year, month, day
    

    【讨论】:

      猜你喜欢
      • 2013-07-19
      • 2017-06-11
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多