Google Bigquery multiple updates based on WHERE - need a solution - where-clause

is there a way to do multiple updates based on other field value
WHERE, not CASE
idea is below
thanks
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = '54620' where substr(zip,1,5) = '99047',
SET CBSA_CODE = '31793' where substr(zip,1,5) = '45700'

A CASE expression is in fact the typical way you would handle this logic:
UPDATE dataset.people
SET CBSA_CODE = CASE SUBSTR(zip, 1, 5)
WHEN '99047' THEN '54620'
WHEN '45700' THEN '31793' END
WHERE
SUBSTR(zip, 1, 5) IN ('99047', '45700');
The only alternative to this which I can see would be to run mutliple update statements, one for each ZIP code value. But that seems unwieldy and undesirable as compared to using a CASE expression.

Use SQL CASE, which is part of Standard SQL (see official BQ docs):
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = CASE
WHEN substr(zip,1,5) = '99047' THEN '54620'
WHEN substr(zip,1,5) = '45700' THEN '31793'
END
WHERE substr(zip,1,5) IN('99047', '45700')

Related

Snowflake ignores statement in where clause where I'm comparing timestamps

so I'm building a SCD type 2 in snowflake, but it ignores the where clause in which is comparision between "to_timestamp" and "expiry_date". Expiry_date is a variable that is set to '9999-08-17 07:31:29.901000000' (as infinity) and To_timestamp is a column in table. I want to query only the rows that have to_timestamp set to infinity (they are still active) but snowflake seems to ignore this part of where clause. Below is some of the code (it should update the rows that are expired - that means change their "to_timestamp" to current time. and it does but it does to rows with timestamps of all kind - it ignores last line)
SET EXPIRY_DATE_NTZ = '9999-08-17 07:31:29.901000000';
SET CURRENT_DATE_NTZ = TO_TIMESTAMP_NTZ(CURRENT_TIMESTAMP());
UPDATE CUSTOMER_TARGET CT
SET CT.TO_TIMESTAMP = $CURRENT_DATE_NTZ
FROM POC.SNOWFLAKE_POC.CUSTOMER_STAGE CS
WHERE CT.C_CUSTOMER_ID = CS.C_CUSTOMER_ID
AND (CT.C_FIRST_NAME <> CS.C_FIRST_NAME OR CT.C_LAST_NAME <> CS.C_LAST_NAME OR CT.C_BIRTH_YEAR
<> CS.C_BIRTH_YEAR OR CT.C_BIRTH_COUNTRY <> CS.C_BIRTH_COUNTRY OR CT.C_LAST_REVIEW_DATE<>CS.C_LAST_REVIEW_DATE)
AND CT.TO_TIMESTAMP = $EXPIRY_DATE_NTZ;
I have two of these update statements (one for updates and one for deletes) and a merge statement for inserts. And it ignores the comparision in every single one, updating the rows that have "to_timestamp" set to something like "2021-08-24 07:11:53.510000000". I've tried every combination possible (between ... and ..., >= ... <=, <=, >=, comparing in "case" statement of update,...) - nothing. What could be the cause/solution?
As we do not know the structure of CUSTOMER_TARGET I would suggest to explicitly set the data type of EXPIRY_DATE_NTZ variable to match the column data type:
SET EXPIRY_DATE_NTZ = '9999-08-17 07:31:29.901000000';
SELECT $EXPIRY_DATE_NTZ;
DESCRIBE RESULT LAST_QUERY_ID();
to:
-- TIMESTAMP_NTZ as an example
SET EXPIRY_DATE_NTZ = '9999-08-17 07:31:29.901000000'::TIMESTAMP_NTZ;
SELECT $EXPIRY_DATE_NTZ;
DESCRIBE RESULT LAST_QUERY_ID();
By doing that way there are no "implicit conversions" involved in the process.
Another advice is usage of IS DISTINCT FROM instead of <>. IS DISTINCT FROM is NULL safe, which is important if columns are defined as nullable.
UPDATE CUSTOMER_TARGET CT
SET CT.TO_TIMESTAMP = $CURRENT_DATE_NTZ
FROM POC.SNOWFLAKE_POC.CUSTOMER_STAGE CS
WHERE CT.C_CUSTOMER_ID = CS.C_CUSTOMER_ID
AND (CT.C_FIRST_NAME IS DISTINCT FROM CS.C_FIRST_NAME
OR CT.C_LAST_NAME IS DISTINCT FROM CS.C_LAST_NAME
OR CT.C_BIRTH_YEAR IS DISTINCT FROM CS.C_BIRTH_YEAR
OR CT.C_BIRTH_COUNTRY IS DISTINCT FROM CS.C_BIRTH_COUNTRY
OR CT.C_LAST_REVIEW_DATE IS DISTINCT FROM CS.C_LAST_REVIEW_DATE)
AND CT.TO_TIMESTAMP = $EXPIRY_DATE_NTZ;
Your SQL does not have any issues with the filters (ORs are surrounded by the brackets etc). I assume that you have checked the execution profile, and did not see your filter (CT.TO_TIMESTAMP = '9999-08-17 07:31:29.901000000'). In this case, all rows in the target table should have this value in the column TO_TIMESTAMP.
I highly recommend you check the data first. If you are running multiple UPDATE/MERGE commands, you may miss that the data has already updated with this value.

Oracle update based on query

I've got the folloing tables:rashodz, naklrashodz, transport, trans_task_load, shipment_plan.
What I need is to update tbl:rashodz.id_ship whith tbl:shipment_plan.id_ship as follows
rashodz.id2 = shipment_plan.id2
and rashodz.nsthet = naklrashodz.nsthet
and naklrashodz.nsthet = trans_task_load.nsthet
and shipment_plan.idts = trans_task_load.idts
Just do not have an idea how to do it.
When there are two tables under consideration there is no problem, but how to do it when multiple tables are involved?
I would appreciate any help.
Is the code snippet you give the condition for the update?
Then how about:
update rashodz r
set id_ship = nvl(
(select id_ship
from shipment_plan,
naklrashodz,
transport,
trans_task_load
where r.id2 = shipment_plan.id2
and r.nsthet = naklrashod.nsthet
and naklrashod.nsthet = trans_task_load.nsthet
and shipment_plan.idts = trans_task_load.idts
), id_ship)
The purpose of the NVL in this case is that if there is no match, id_ship remains unchanged from its original value.

Use of CASE in Update query

I have following SQL (Using MS SQL)
UPDATE AR_Slots
SET Running = #Running,
StopSignal = #StopSignal,
WHERE (SlotId = #SlotId)
I want to set a field called RunListID to 0 if Running parameter (boolean) is true otherwise I don't want to change the value at all.
What is the "correct" way of doing this?
Thanks,
Stefan
Try something like
UPDATE AR_Slots
SET RunListID =
CASE
WHEN <your check value>
THEN 0
ELSE RunListID
END,
<your other sets>
WHERE <your criteria>

UPDATE is not allowed because the statement updates view "table_name" which participates in a join and has an INSTEAD OF UPDATE trigger

I am getting the following error while executing the following query in an Stored Procedure. Could anyone help in finding the fault?
UPDATE is not allowed because the statement updates view "sup_item" which participates in a join and has an INSTEAD OF UPDATE trigger.
UPDATE si
SET
name = mc.name,
sup_item_cat_id = mc.res_sup_item_cat_id,
xf_value = mc.xf_value,
ava_start_date = mc.ava_start_date,
ava_end_date = mc.ava_end_date,
status_code = mc.status_code,
last_mod_us_id = CASE WHEN mc.last_mod_us_id = 42 THEN #posting_us_id
ELSE mc.last_mod_us_id END,
last_mod_tsp = CURRENT_tsp
FROM sup_item AS si
JOIN merch_cat_imp_sup_item AS mc
ON mc.sup_id = si.sup_id
AND mc.res_sup_item_id = si.sup_item_id
AND mc.cat_imp_event_id = #cat_imp_event_id
AND mc.accept_flag = 'y'
WHERE si.shi_flag = 'n'
I found the reference: http://msdn.microsoft.com/en-us/library/ms177523.aspx
A view with an INSTEAD OF UPDATE trigger cannot be a target of an
UPDATE with a FROM clause.
So, I have to rewrite the UPDATE statement (it still can be in a procedure) to NOT use sup_item (which is a view), but keep the underlying table(s) as needed.
Could someone please rewrite it, if anyone knows what to do?
You can use MERGE to achieve this. Try:
MERGE INTO sup_item si
USING merch_cat_imp_sup_item AS mc
ON mc.sup_id = si.sup_id
AND mc.res_sup_item_id = si.sup_item_id
AND mc.cat_imp_event_id = #cat_imp_event_id
AND mc.accept_flag = 'y'
AND si.shi_flag = 'n'
WHEN MATCHED
THEN UPDATE
SET
name = mc.name,
sup_item_cat_id = mc.res_sup_item_cat_id,
xf_value = mc.xf_value,
ava_start_date = mc.ava_start_date,
ava_end_date = mc.ava_end_date,
status_code = mc.status_code,
last_mod_us_id = CASE WHEN mc.last_mod_us_id = 42 THEN #posting_us_id
ELSE mc.last_mod_us_id END,
last_mod_tsp = CURRENT_tsp
The issue is not within your query. As per comments on your question, the entity you are updating [sup_item], isn't actually a table, it's a view. That view has an INSTEAD OF UPDATE trigger on it.
Are you able to post the SQL for the View and for the Trigger(s)?
I would also be interested, because I have a stored procedure in a database that I have inherited which tries to do this. It won't let me create the sproc in SQL 2014, but the fact that it is there in the sproc indicates to me that an earlier version of SQL server must have allowed this.
Maybe in earlier versions your procedure operated on a table, which was later replaced by a view.
You should replace your "update from" syntax by standard ANSI syntax of update.

Use a specific database depending on condition

Can I somehow use a specific database given a specific condition? To clarify I will give a naive example:
CASE
WHEN #dbnum = 1 THEN USE Db1
ELSE USE DefaultDb
END
You can do it with an IF:
IF #dbnum = 1
USE Db1;
ELSE
USE DefaultDb;