简体   繁体   中英

Bigquery: Update column based on condition set in another table

I have two tables t1 and 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.

Table t1:

obid blocked
1
2

Table t2:

obid inact objval
1 1000
2 2000

Expected output: The table t1 should now look like this

obid blocked
1 X
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? 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.

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 ".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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