【问题标题】:Filling the null values of a column based on a column with equal values根据具有相等值的列填充列的空值
【发布时间】:2021-06-12 00:49:57
【问题描述】:

我有一张如下表:

customerId orderNo transactionCode transactionSubCode
123123123 ABC-1234 null null
123123123 XYZ-123 null null
123123123 DEF-456 null null
123123123 HYT-111 12a8ksabbc 999123xxxx11
123123123 ZZZ-999 null null
333333334 XYC-777 null null
333333334 XYZ-331 null null
333333334 XYZ-334 13a7kcssaf null
333333334 XYZ-655 null 76612yyyas33

我想将 transactionSubCode 列上的现有值分配给同一列的所有空值,基于具有相同值的列,即 customerId 在我们的例子中。所以决赛桌的结果如下:

customerId orderNo transactionCode transactionSubCode
123123123 ABC-1234 null 999123xxxx11
123123123 XYZ-123 null 999123xxxx11
123123123 DEF-456 null 999123xxxx11
123123123 HYT-111 12a8ksabbc 999123xxxx11
123123123 ZZZ-999 null 999123xxxx11
333333334 XYC-777 null 76612yyyas33
333333334 XYZ-331 null 76612yyyas33
333333334 XYZ-334 13a7kcssaf 76612yyyas33
333333334 XYZ-655 null 76612yyyas33

我尝试过使用不同的方法进行自连接,但无论如何都无法获得预期的结果。

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    你可以使用窗口函数:

    select t.* (except transactionSubCode),
           max(transactionSubCode) over (partition by customerId) as transactionSubCode
    from t;
    

    【讨论】:

    • 先生,这个答案非常快。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 2022-01-15
    • 1970-01-01
    • 2023-02-05
    • 2020-12-27
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多