通过join或其他方式基于匹配的id在表列中填充空行

tsm1rwdh  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(286)

我正在vertica数据库中使用sql。
假设我有两张table:a桌和b桌。我们还假设表a是我的最终/主表,用于tableau(或类似的东西)中的数据vis,表b根据第三级表(表c)中的匹配将某些列馈送到表a中(与此对话无关)。
按原样,表a有以下列:

ProgramName [varchar(50)]
CustomerName [varchar(50)]
Total_Cost [numeric(18,4)]

按原样,表b有以下列:

CustomerCode [varchar(10)]
Total_Cost [numeric(18,4)]

我想做的是更新表a的customername列,使之等于表b中的customercode,其中total\u cost\u dollars的列在表中彼此相等。
我运行这个左连接查询是为了确保,当我将表a的customername更新为等于customercode时,total cost列与我的整个数据集完全匹配。

SELECT
    A.ProgramName,
    A.CustomerName,
    A.total_cost_dollars,
    B.CustomerCode,
    B.total_cost_dollars
FROM
    TableA A
LEFT JOIN
    TableB B
ON
    B.total_cost_dollars = A.total_cost_dollars
WHERE
    A.CustomerName IS NULL;

你知道怎么解决这个问题吗?

bhmjp9jg

bhmjp9jg1#

由于vertica支持合并查询,因此可以使用merge语句:

merge into TableA A
using TableB B
ON (B.total_cost_dollars = A.total_cost_dollars)
when matched then
update
set
A.CustomerName = B.CustomerCode
where
A.CustomerName IS NULL;

相关问题