changing column values in bigquery

回眸只為那壹抹淺笑 提交于 2020-05-24 07:31:07

问题


I have a table where there is a column with different values like America, South Korea, Japan and so on. I would like to replace the values with America=USA, South Korea=SA, Japan= JP like these. What would be the code?


回答1:


The best way to probably handle this would be to maintain a separate table which maps full country names to their two letter codes:

country_full | country_abbr
America      | USA
South Korea  | SA
Japan        | JP

Then, you may join your current table to this lookup table to bring in the codes:

SELECT
    t1.*,
    t2.country_abbr
FROM yourTable t1
LEFT JOIN country_codes t2
    ON t1.country = t2.country_full;

Another way to handle this, though not very scalable, would be to use a CASE expression to bring in the codes:

SELECT
    country,
    CASE country WHEN 'America'     THEN 'USA'
                 WHEN 'South Korea' THEN 'SA'
                 WHEN 'Japan'       THEN 'JP'
                 ELSE 'Unknown' END As code
FROM yourTable;


来源:https://stackoverflow.com/questions/61073333/changing-column-values-in-bigquery

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