Google Bigquery multiple updates based on WHERE - need a solution

空扰寡人 提交于 2019-12-11 16:04:40

问题


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'

回答1:


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.




回答2:


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')


来源:https://stackoverflow.com/questions/50105187/google-bigquery-multiple-updates-based-on-where-need-a-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!