简体   繁体   English

Bigquery:根据另一个表中设置的条件更新列

[英]Bigquery: Update column based on condition set in another table

I have two tables t1 and t2.我有两个表 t1 和 t2。 I need to join them and set the blocked column value in t1 as 'X' when t2.inact = '' (is empty) and t2.objval = 1000. Both the tables are joined by obid.我需要加入它们并在 t2.inact = ''(为空)和 t2.objval = 1000 时将 t1 中的阻塞列值设置为 'X'。这两个表都由 obid 连接。

Table t1:表 t1:

obid遵守 blocked封锁
1 1个
2 2个

Table t2:表 t2:

obid遵守 inact不作为 objval客观的
1 1个 1000 1000
2 2个 2000 2000

Expected output: The table t1 should now look like this预期 output:表 t1 现在应该如下所示

obid遵守 blocked封锁
1 1个 X X
2 2个

In bigquery it is said that it is not possible to use WITH CTE's along with update statement which was my first try..What could be the other way possible?在 bigquery 中,据说不可能将 WITH CTE 与更新语句一起使用,这是我的第一次尝试。其他方法可能是什么? Below is my another SQL attempt with CASE and this is creating a new column called blocked...but the requirement is filling the data in already present column blocked.下面是我对 CASE 的另一个 SQL 尝试,这是创建一个名为 blocked 的新列...但要求是在已存在的列中填充数据 blocked。

WITH abc AS(
SELECT obid,blocked
FROM table1),
def AS (
SELECT obid,inact,objval,
FROM table2
WHERE objval = '1000')

SELECT CASE WHEN t2.inact = '' THEN 'X'
        ELSE '' END as blocked
        FROM abc t1
JOIN def t2
ON t2.obid = t1.obid

Any help appreciated!!!任何帮助表示赞赏!

You can still use an UPDATE statement on the " t1 " table, while checking conditions on the " t2 " table, thus simulating a join between " t1 " and " t2 ".您仍然可以在“ t1 ”表上使用UPDATE语句,同时检查“ t2 ”表上的条件,从而模拟“ t1 ”和“ t2 ”之间的连接。

UPDATE t1
SET blocked = 'X'
FROM t2
WHERE t1.obid = t2.obid AND t2.inact = '' AND t2.objval = 1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM