【问题标题】:How to populate null column values w/ next non-null value如何使用下一个非空值填充空列值
【发布时间】:2021-09-05 14:40:54
【问题描述】:

我有一个交互表,其中每个交互都分配了一个 id_agent。有些行的 id_agent 具有空值,我想为其分配下一个非空值。我可以使用下一行的前导函数来执行此操作,但是,我有多个连续行的实例,其中 id_agent 为空

当前数据:

id_agent ts_end_utc inbound_or_outbound interaction_channel id_ticket
-2 2020-09-15 9:40:04 Inbound supportbot 93789075
299210763 2020-10-10 16:00:23 Outbound messaging 93789075
Null 2020-10-12 8:46:05 Inbound messaging 93789075
Null 2020-10-12 10:38:07 Inbound messaging 93789075
Null 2020-10-15 8:25:23 Inbound messaging 93789075
234926893 2020-10-15 17:45:58 Outbound messaging 93789075
299210763 2020-10-28 19:05:49 Outbound messaging 93789075
Null 2020-11-09 6:50:13 Inbound messaging 93789075
299210763 2020-11-09 13:03:05 Outbound messaging 93789075

期望状态:

id_agent ts_end_utc inbound_or_outbound interaction_channel id_ticket
-2 2020-09-15 9:40:04 Inbound supportbot 93789075
299210763 2020-10-10 16:00:23 Outbound messaging 93789075
234926893 2020-10-12 8:46:05 Inbound messaging 93789075
234926893 2020-10-12 10:38:07 Inbound messaging 93789075
234926893 2020-10-15 8:25:23 Inbound messaging 93789075
234926893 2020-10-15 17:45:58 Outbound messaging 93789075
299210763 2020-10-28 19:05:49 Outbound messaging 93789075
299210763 2020-11-09 6:50:13 Inbound messaging 93789075
299210763 2020-11-09 13:03:05 Outbound messaging 93789075

我知道以后如何使用 case 语句使列看起来符合要求,但我将如何设置逻辑/函数以获取下一个非空值?

【问题讨论】:

    标签: mysql sql hive hiveql window-functions


    【解决方案1】:

    你可以使用last_value():

    select t.*,
           last_value(id_agent, true) over (order by ts_end_utc) as imputed_id_agent
    from t;
    

    【讨论】:

    • 我不相信 last_value 可以带参数 1
    • @HarryBarsegyan。 . .您可能需要改用true。出于某种原因,Hive 支持此功能,但不支持应该实现它的标准 ignore nulls 关键字。
    • 另一件事是这不是数据的结尾。这是一个持续不断的大数据集,并且不断出现类似的模式(我刚刚编辑了示例数据)@gordonlinoff
    猜你喜欢
    • 2021-10-15
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多