【问题标题】:Replace string from another table替换另一个表中的字符串
【发布时间】:2023-01-25 19:27:57
【问题描述】:

假设我有 addressescountry_codes 表。
addresses表有不正确的country_code值,我想根据地址用country_codes表中的country_code替换它们。
如何在雪花中做到这一点?

请注意,address 列中可能包含国家名称或国家代码。

表:

+--------------+-----------------------------------+
| country_code |              address              |
+--------------+-----------------------------------+
| US           | 1145 Oakmound Drive, US           |
| W            | 4733 Pallet Street, United States |
| F            | Rua Wanda Carnio 190, Brazil      |
| 22           | Via delle Viole 137, Italy        |
| 50           | 7 Essex Rd, GB                    |
+--------------+-----------------------------------+

+--------------+----------------+
| country_code |    country     |
+--------------+----------------+
| GB           | United Kingdom |
| BR           | Brazil         |
| IT           | Italy          |
| US           | United States  |
+--------------+----------------+

期望的输出:

+--------------+-----------------------------------+
| country_code |              address              |
+--------------+-----------------------------------+
| US           | 1145 Oakmound Drive, US           |
| US           | 4733 Pallet Street, United States |
| BR           | Rua Wanda Carnio 190, Brazil      |
| IT           | Via delle Viole 137, Italy        |
| GB           | 7 Essex Rd, GB                    |
+--------------+-----------------------------------+

【问题讨论】:

    标签: sql snowflake-cloud-data-platform


    【解决方案1】:

    这样的事情可以给你预期的结果:

    select c.country_code, a.address from addresses a join country_codes c
    ON c.country_code = a.country_code OR ( POSITION ( c.country_code, a.address ) OR POSITION ( c.country, a.address )); 
    
    +--------------+-----------------------------------+
    | COUNTRY_CODE |              ADDRESS              |
    +--------------+-----------------------------------+
    | US           | 1145 Oakmound Drive, US           |
    | US           | 4733 Pallet Street, United States |
    | BR           | Rua Wanda Carnio 190, Brazil      |
    | IT           | Via delle Viole 137, Italy        |
    | GB           | 7 Essex Rd, GB                    |
    +--------------+-----------------------------------+
    

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2013-12-03
      • 2019-05-14
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2017-03-07
      相关资源
      最近更新 更多