根据具有相等值的列填充列的空值 [英] Filling the null values of a column based on a column with equal values

查看:21
本文介绍了根据具有相等值的列填充列的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格如下:

<头>
客户 ID订单号交易代码transactionSubCode
123123123ABC-1234
123123123XYZ-123
123123123DEF-456
123123123HYT-11112a8ksabbc999123xxxx11
123123123ZZZ-999
333333334XYC-777
333333334XYZ-331
333333334XYZ-33413a7kcssaf
333333334XYZ-65576612yyyas33

我想根据具有相同值的列(即 customerId<)将 transactionSubCode 列上的现有值分配给同一列的所有空值/strong> 在我们的例子中.所以决赛桌的结果如下:

I would like to distribute the existing value on the transactionSubCode column to all of the null values of the same column, based on a column that has the same value, which is customerId in our case. So the final table will have results like below:

<头>
客户 ID订单号交易代码transactionSubCode
123123123ABC-1234999123xxxx11
123123123XYZ-123999123xxxx11
123123123DEF-456999123xxxx11
123123123HYT-11112a8ksabbc999123xxxx11
123123123ZZZ-999999123xxxx11
333333334XYC-77776612yyyas33
333333334XYZ-33176612yyyas33
333333334XYZ-33413a7kcssaf76612yyyas33
333333334XYZ-65576612yyyas33

我尝试过使用不同的方法进行自连接,但无论如何都没有得到想要的结果.

I have tried self-joining with different approaches but never managed to get the desired result in any case.

推荐答案

可以使用窗口函数:

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

这篇关于根据具有相等值的列填充列的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆