Table 更新基于 match/nomatch
Table update based on match/nomatch
我有两个表 t1
和 t2
。
t1
具有以下结构:
yearmonth
account
company
RCV_amount_t1
t2
具有以下结构:
yearmonth
account
company
billing amount
billing amount CM_1
billing amount CM_2
RCV_amount_t2
我想使用 yearmonth
、account
和 company
加入 t2
到 t1
。如果它们匹配,我想用 RCG_amount_t1
中的值更新 RCV_amount_t2
。否则,我想将 RCV_amount_t2
设置为空格。
以同样的方式,我想使用 yearmonth
、account
和 company
加入 t1
和 t2
并相应地设置值。
是否可以实现?如果是这样,我该怎么做?
您需要使用 MERGE
。
它允许您连接两个表并指定如果它们匹配则如何更新值。
MERGE
语句的一般结构如下所示:
MERGE INTO driver_table
USING other_table
ON
(
driver_table.column1 = other_table.column1
AND driver_table.column2 = other_table.column2
AND ...
)
WHEN MATCHED THEN UPDATE
SET
driver_table.some_column = other_table.some_value,
driver_table.some_flag = 'Y',
...
;
I want to join t2 to t1 using yearmonth, account, and company. If they
match, I want to update RCV_amount_t2 with the value in RCG_amount_t1.
Otherwise, I want to set RCV_amount_t2 to spaces.
这将使用适当的值更新匹配行,并将不匹配的行更新为 NULL
。如果该字段是数字,则不能将其更新为"spaces"; NULL
将是没有价值的适当指标。如果该字段不是数字,那么您可以进行第二次更新以将 NULL
值替换为您喜欢的任何值,但在我看来 NULL
仍然是最合适的无值指标。
UPDATE t2 SET rcv_amount_t2 = (
SELECT rcv_amount_t1
FROM t1
WHERE t1.yearmonth = t2.yearmonth
AND t1.account = t2.account
AND t1.company = t2.company
)
看来我们无法在单个查询中解决它,我们需要一个 merge and a correlated query,它对我来说很好用:
这将在匹配时使用 t1 中的值更新 t2:
MERGE INTO t2
USING (SELECT yearmonth, account, company, RCV_amount_t1 FROM t1) S
ON (t1.yearmonth = t2.yearmonth and
t1.account = t2.account and
t1.company = t2.company)
WHEN MATCHED THEN UPDATE SET t2.RCV_amount_t2 = S.RCV_amount_t1;
然后一个包含相关子查询的查询在不匹配时为空:
update t2 set RCV_amount_t2 = ' ' where yearmonth||account||company not in(
select yearmonth||account||company from t1
where t1.yearmonth = t2.yearmonth and t1.account=t2.account and t1.company=t2.company);
我有两个表 t1
和 t2
。
t1
具有以下结构:
yearmonth
account
company
RCV_amount_t1
t2
具有以下结构:
yearmonth
account
company
billing amount
billing amount CM_1
billing amount CM_2
RCV_amount_t2
我想使用 yearmonth
、account
和 company
加入 t2
到 t1
。如果它们匹配,我想用 RCG_amount_t1
中的值更新 RCV_amount_t2
。否则,我想将 RCV_amount_t2
设置为空格。
以同样的方式,我想使用 yearmonth
、account
和 company
加入 t1
和 t2
并相应地设置值。
是否可以实现?如果是这样,我该怎么做?
您需要使用 MERGE
。
它允许您连接两个表并指定如果它们匹配则如何更新值。
MERGE
语句的一般结构如下所示:
MERGE INTO driver_table
USING other_table
ON
(
driver_table.column1 = other_table.column1
AND driver_table.column2 = other_table.column2
AND ...
)
WHEN MATCHED THEN UPDATE
SET
driver_table.some_column = other_table.some_value,
driver_table.some_flag = 'Y',
...
;
I want to join t2 to t1 using yearmonth, account, and company. If they match, I want to update RCV_amount_t2 with the value in RCG_amount_t1. Otherwise, I want to set RCV_amount_t2 to spaces.
这将使用适当的值更新匹配行,并将不匹配的行更新为 NULL
。如果该字段是数字,则不能将其更新为"spaces"; NULL
将是没有价值的适当指标。如果该字段不是数字,那么您可以进行第二次更新以将 NULL
值替换为您喜欢的任何值,但在我看来 NULL
仍然是最合适的无值指标。
UPDATE t2 SET rcv_amount_t2 = (
SELECT rcv_amount_t1
FROM t1
WHERE t1.yearmonth = t2.yearmonth
AND t1.account = t2.account
AND t1.company = t2.company
)
看来我们无法在单个查询中解决它,我们需要一个 merge and a correlated query,它对我来说很好用:
这将在匹配时使用 t1 中的值更新 t2:
MERGE INTO t2
USING (SELECT yearmonth, account, company, RCV_amount_t1 FROM t1) S
ON (t1.yearmonth = t2.yearmonth and
t1.account = t2.account and
t1.company = t2.company)
WHEN MATCHED THEN UPDATE SET t2.RCV_amount_t2 = S.RCV_amount_t1;
然后一个包含相关子查询的查询在不匹配时为空:
update t2 set RCV_amount_t2 = ' ' where yearmonth||account||company not in(
select yearmonth||account||company from t1
where t1.yearmonth = t2.yearmonth and t1.account=t2.account and t1.company=t2.company);