Using update with Left Join BigQuery - google-bigquery

I am trying to write an Update query with LEFT JOIN in BigQuery but I am not sure how to write it.
update Table1
set ColumnTest = ifnull(b.value, 'no run')
From left join (select distinct ID,value FROM Table2 where value = 10) B --
where Table1.ID= Table2.ID
I have 2 tables Table1 and Table2
I want to update Table1.ColumnTest with Table2.Value where Table1.ID= Table2.ID
and if Table1 <> Table2 then update Table1.ColumnTest with 'no run'
Thanks!!
New Try
UPDATE Table1
SET LP = IFNULL(t2.value, 'no run')
FROM ( select distinct hits.eventInfo.eventCategory as ID, value
FROM Table2
CROSS JOIN UNNEST (hits) AS hits
left join TAble1 using (hits.eventInfo.eventCategory)
WHERE) t2
WHERE t1.ID = t2.ID
Error: Syntax error: Expected ")" or "," but got "."

I want to update Table1.ColumnTest with Table2.Value where Table1.ID= Table2.ID and if Table1 <> Table2 then update Table1.ColumnTest with 'no run'
Below is for BigQuery Standard SQL
UPDATE `table1` t1
SET ColumnTest = IFNULL(value, 'no run')
FROM (
SELECT id, value
FROM `table1`
LEFT JOIN `table2`
USING(id)
) t2
WHERE t1.id = t2.id

Related

Using update with Left Join and cross join BigQuery

I have a followup question on my already posted question.
Using update with Left Join BigQuery
I amt trying to achieve the same result but also adding Cross Join in it.
update Table1
set ColumnTest = ifnull(b.value, 'no run')
From left join
(select distinct h.eventinfo.eventcategory as ID,value
FROM Table2
cross join (hits)h )
where Table1.ID= Table2.ID
I have 2 tables Table1 and Table2
I want to update Table1.ColumnTest with Table2.Value
where Table1.ID= Table2.hits.eventInfo.eventCategory (Unnest Table2)
and if Table1.ID <> Table2.hits.eventInfo.eventCategory then update Table1.ColumnTest with 'no run'
Thanks for you help!!
Try below
UPDATE `table1` t1
SET ColumnTest = IFNULL(t2.value, 'no run')
FROM (
SELECT id, value
FROM `table1`
LEFT JOIN (
SELECT hit.eventInfo.eventCategory AS id, value
FROM `table2`
CROSS JOIN UNNEST (hits) AS hit
)
USING(id)
) t2
WHERE t1.ID = t2.ID

UPDATE with isNull

In Microsoft SQL Server,
if the inner select doesn't have a matching criteria, then I need to update Field1 with blank instead of null.
UPDATE Table1
SET Field1=(
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
)
Please try like this -
UPDATE a
SET a.Field1 = ISNULL(b.Field2,'')
FROM Table1 a
LEFT JOIN Table2 b ON b.Field3 = a.Field4
You can reference another table using SQL Server's UPDATE ... FROM ... syntax:
UPDATE t1
SET field1 = COALESCE(t2.field2, '')
FROM Table1 t1
LEFT JOIN
Table2 t2
ON t2.Field3 = t1.Field4
The COALESCE() function returns the first non-null expression in a list. You can keep most of your original query, by using COALESCE() to replace any NULL from that query with an empty string.
UPDATE Table1
SET Field1=COALESCE((
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
), '')
Try this
UPDATE T1
SET T1.Field1=isnull(T2.Field2,'')
from Table1 T1 left join Table2 T2
ON T2.Field3 = T1.Field4

Oracle: Update based on condition

I want to update a column of table based on a condition. It should check if the value exists in other table, if exists then value from other table will be used else value from same table will be used.
Update table1
Set column1=(select t2.alias||’#email.com’ as new_name
From table2 t2, table1 t1, table3 t3
Where t1.id=t2.id
And t1.id=t3.id
Else if
Select t2.alias is null or t2.id is null
then column1= select t1.id||’#email.com’ as new_name
Any suggestions on this??
Thanks in advance.
Does this do what you want?
Update table1
Set column1 = (select (case when t2.alias is not null and t2.id is not null then t2.alias
else t1.id
end) ||'#email.com' as new_name
From table1 t1 left outer join
table2 t2
on t1.id=t2.id
);
I removed table3 because it does not seem to be used. With left join is won't even be filtering any results.
By leaving out the "insert" half of the merge statement, you can make it into a strictly update statement:
MERGE INTO table1 t
USING(
SELECT t2.id, t2.Alias
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
AND t1.Alias <> t2.Alias
) tu
on( tu.id = t.id )
WHEN MATCHED THEN
update set t.Alias = tu.Alias;
The query will return only those existing values from table2 that differ from table1. To make it exactly match your requirement (update table1 if any value exists in table2) then remove the line AND t1.Alias <> t2.Alias, but why update a field to the same value it already has?

sql left outer join order by column in left table but preserve all rows

I have the following:
SELECT
table1.id
FROM
table1
LEFT OUTER JOIN table2 ON table2.table1_id = table1.id
WHERE
(table1.entry_id=2)
AND
parent_id=0
ORDER BY
SUM(table2.column1) - SUM(table2.column2)
Works fine until I add the 'order by', I need it to get all relevant rows from table1, even if they have unmatched rows in table2, ordering would place them at the bottom.
Try this instead:
SELECT
t1.id
FROM table1 AS t1
LEFT OUTER JOIN
(
SELECT table1_id, SUM(column1) sum1, SUM(column2) sum2
FROM table2
GROUP BY table1_id
) AS t2 ON t2.table1_id = t.id
AND t1.entry_id = 2
AND t1.parent_id = 0
ORDER BY t2.sum1 - t2.sum2;

Using an inner join with subqueries in an update syntax

I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value