[英]Update name in Snowflake variant column
I have copied some json files into Snowflake from a stage and I have a property name which contains a hyphen.我已将一些 json 文件从舞台复制到雪花中,并且我有一个包含连字符的属性名称。
When I try to query for this property name (as shown below), I get this error.当我尝试查询此属性名称时(如下所示),我收到此错误。
select my_variant:test-id from mytable;
SQL compilation error: error line 1 at position 44 invalid identifier 'ID'.
I assume it doesn't like the hyphen.我认为它不喜欢连字符。 Is there any way I can rename this hyphenated name in my variant column so I don't get the error?
有什么方法可以在我的变体列中重命名这个连字符的名称,这样我就不会收到错误消息?
You just need to quote the column name in the variant:您只需要在变体中引用列名:
select my_variant:"test-id" from mytable;
If you want to update it, see below.如果您想更新它,请参见下文。 It assumes that you have a key per row, so that we can aggregate it back to rebuild the variant at the row level.
它假定您每行都有一个键,以便我们可以将其聚合回来以在行级别重建变体。
Setup test table:设置测试表:
create or replace table test (k int, a variant);
insert into test
select 1, parse_json('{"test-id": 1, "test-id2": "2"}')
union all
select 2, parse_json('{"test-1": 1, "test-2": "2"}');
select * from test;
+---+-------------------+
| K | A |
|---+-------------------|
| 1 | { |
| | "test_id": 1, |
| | "test_id2": "2" |
| | } |
| 2 | { |
| | "test_1": 1, |
| | "test_2": "2" |
| | } |
+---+-------------------+
Update the table:更新表格:
update test t
set t.a = b.value
from (
with t as (
select
k,
replace(f.key, '-', '_') as key,
f.value as value
from test,
lateral flatten(a) f
)
select
k, object_agg(key, value) as value
from t
group by k
) b
where t.k = b.k
;
select * from test;
+---+-------------------+
| K | A |
|---+-------------------|
| 1 | { |
| | "test_id": 1, |
| | "test_id2": "2" |
| | } |
| 2 | { |
| | "test_1": 1, |
| | "test_2": "2" |
| | } |
+---+-------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.