简体   繁体   English

如何更新标准 SQL 中查询创建的表?

[英]How to update a table created by query in Standard SQL?

I have created a table in Standard SQL.我在标准 SQL 中创建了一个表。 In this table, I have a column named COUNTRY.在此表中,我有一个名为 COUNTRY 的列。 In the results, I saw that I have Brazil and Brasil, and also United Kingdom and Great Britan, so I want to update one of them to match the other, because it is the same for the analysis I want to do.在结果中,我看到我有巴西和巴西,还有英国和大不列颠,所以我想更新其中一个以匹配另一个,因为我想做的分析是一样的。 Any ideas?有任何想法吗?

You can update a table:您可以更新表:

update t
    set country = (case when country = 'Brasil' then 'Brazil'
                        when country = 'Great Britain' then 'United Kingdom'
                        else country
                   end)
    where country in ('Brasil', 'Great Britain');

However, it might be simpler to modify your query so the next time you create the table the countries will be correct.但是,修改查询可能更简单,因此下次创建表时,国家/地区将是正确的。

If i understand correctly what you want, you can use:如果我正确理解你想要什么,你可以使用:

UPDATE yourTable
SET COUNTRY = 'Great Britan'
WHERE COUNTRY = 'United Kingdom'

This changes every 'United Kingdom' column to 'Great Britan'这会将每个“英国”列更改为“大不列颠”

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

update your_table t
set country = d.country 
from (
  select 'Brazil' country, 'Brasil' matches union all
  select 'United Kingdom', 'UK|Great Britain|GB' union all
  select 'USA', 'US'
) d 
where t.country in unnest(split(matches, '|'));

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

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