雪花查询存储为数组的嵌套 JSON

时间:2021-03-14 16:10:58

标签: snowflake-cloud-data-platform

我已经运行以下代码在表中生成一个数组

CREATE OR REPLACE TABLE PROVIDER_TABLE_V8 AS 
SELECT "rmpostcode", 
array_agg(object_delete(object_construct(f.*),'rmpostcode') ) within group (order by "name") AS "providers"
FROM FASTEST_DOWN_STR_V8 f
GROUP BY "rmpostcode";

我现在正在尝试检查结果表是否完成了我认为它已经完成的事情...但我不确定如何查询结果对象。尝试横向展平等失败,因为它不再是 JSON,是吗?

编辑:感谢@Rob Silva,我已将数据类型更改为 VARIANT,这对创建此表有所帮助。

我拥有的对象是

[ { "fastest_down": "0.00", "name": "B4RN", "present": "0" }, { "fastest_down": "0.00", "name": "Gigaclear", "present": "0" }, { "fastest_down": "0.00", "name": "OFNL (IFNL)", "present": "0" }, { "fastest_down": "0.00", "name": "airband_fibre", "present": "0" }, { "fastest_down": "0.00", "name": "balquhidder", "present": "0" }, { "fastest_down": "0.00", "name": "blackfibre", "present": "0" }, ...]

那是一行。

我想要做的是为指定的 present = 1 找到 name 的所有行,但我正在努力查询嵌套的 JSON 对象。

2 个答案:

答案 0 :(得分:1)

<块引用>

尝试横向展平等失败,因为它不再是 JSON, 是吗?

是的。您可以将其转到 jsonlint.com 并进行验证。

以下是如何逐步完成此工作的细分:

create table foo (v variant);

-- Insert your data
insert into foo select parse_json('<your json>');

-- See how it looks raw.
select * from foo;

-- Now flatten the array with a lateral join.
-- Note the addition of metadata columns from the
-- output of the flatten table function
select * from foo, lateral flatten(v);

-- Filter rows to where present = 1
-- Parse using the pattern COLUMN_NAME:json_property::cast_type
select * from foo, lateral flatten(v) where VALUE:present::int = 1;

-- Clean up and alias the names, etc.
select   VALUE:fastest_down::float      as FASTEST_DOWN
        ,VALUE:name::string             as "NAME"
        ,VALUE:present::int             as PRESENT
from   foo, lateral flatten(v)
-- Optionally add your where clause on PRESENT
;

答案 1 :(得分:0)

Chris - 尝试使用 parse_json(providers) 并查看结果对象是否可以按您期望的方式展平。