【问题标题】:How to merge a literal table into an existing table in snowflake, using SQL如何使用 SQL 将文字表合并到雪花中的现有表中
【发布时间】:2021-08-04 02:27:33
【问题描述】:

我正在尝试将数据合并到我数据库中的现有表中。我要合并的数据不在现有表中,所以我想在我的 SQL 查询中将它作为表文字提供给我的数据库。以下是我目前得到的代码 - 它不断出错并显示一条消息:

SQL compilation error: error line 8 at position 39 invalid identifier 'VALS.COL2'

merge into existing_table
    using 
        (select col1, col2, parse_json(col3) from
            values 
            ('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$), 
            ('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
            as vals(col1, col2, col3))
    on existing_table.sales_order_id = vals.col2
when matched then 
    update set 
        existing_table.timestamp_utc = vals.colOne, existing_table.JSON = vals.colThree
when not matched then
    insert (timestamp_utc, sales_order_id, JSON)
    values (vals.colOne, vals.colTwo, vals.colThree);

请注意,我正在使用 Snowflake 应用程序来保存数据 - Snowflake 应用程序允许使用 parse_json() 函数。

【问题讨论】:

    标签: python merge snowflake-cloud-data-platform


    【解决方案1】:

    除了上一个答案中已经更正的缺少表别名(使用 src 作为别名)之外,还有一些错误/错别字:

    • parse_json(col3) 需要一个别名,添加为 col3
    • 不一致的列命名,在源查询中列被命名为 col1、col2、col3 但在连接条件中列被称为 colOne、colTwo、colThree

    这是正确运行且没有错误的查询

    merge into existing_table
        using 
            (select col1, col2, parse_json(col3) as col3 from
                values 
                ('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$), 
                ('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
                as vals(col1, col2, col3)) src
        on existing_table.sales_order_id = src.col2
    when matched then 
        update set 
            existing_table.timestamp_utc = src.col1, existing_table.JSON = src.col3
    when not matched then
        insert (timestamp_utc, sales_order_id, JSON)
        values (src.col1, src.col2, src.col3);
    

    【讨论】:

    • 非常感谢!这与第一个答案相结合,让我到达了我需要去的地方。 col1 与 colOne 的拼写错误以及 col3 上的别名绝对是问题的一部分。您提供的这个解决方案效果很好。
    【解决方案2】:

    我认为值表的别名在子选择中,然后需要一个别名。我没有对此进行测试,并且它的格式不能很好地作为评论,所以如果它不起作用,我将删除这个答案:

    merge into existing_table
        using 
            (select col1, col2, parse_json(col3) from
                values 
                ('2021-08-03 17:38:53.977484+00:00', '34o234j3', $${"data":"here's some data","data2":"Here's some more data"}$$), 
                ('2021-08-02 08:38:55', '1934802h32', $${"data":"here's some dataX","data2":"Here's some more dataY"}$$)
                as vals(col1, col2, col3)) src
        on existing_table.sales_order_id = src.col2
    when matched then 
        update set 
            existing_table.timestamp_utc = src.colOne, existing_table.JSON = src.colThree
    when not matched then
        insert (timestamp_utc, sales_order_id, JSON)
        values (src.colOne, src.colTwo, src.colThree);
    

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2021-09-05
      • 2020-06-27
      相关资源
      最近更新 更多