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

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

问题描述

我有一张桌子,如下:

<身体>
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 .因此,决赛桌的结果将如下所示:

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:

<身体>
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

我曾经尝试过用不同的方法进行自我加入,但是在任何情况下都无法获得预期的结果.

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天全站免登陆