雪花查询元素数组

时间:2020-09-02 17:35:40

标签: snowflake-cloud-data-platform snowflake-schema snowflake-task

我正在使用SNOW_FLAKE并尝试查询以列名nested_colmn下的元素数组形式存储的数据,例如:

nested_colmn
[
  {
    "firstKey": "val1",
    "secondKey": 2555,
    "thirdKey": false,
    "fourthkey": "otrvalue"
  },
  {
    "firstKey": "val2",
    "secondKey": 255221,
    "thirdKey": true,
    "fourthkey": "otrvalu"
  }
]

如果我这样做,上面的数组将作为完整的一行返回

Select nested_colmn from table_name

现在,我只想查询/获取“属性”列中 firstkey (nested_colmn.firstkey)的结果。如何将查询框架化为从数组中检索单个自定义元素,而不是全部获取。如果对此有任何想法,请帮助我

2 个答案:

答案 0 :(得分:0)

注意:我将假设您确实希望源表将数组作为值,而不是剥离外部数组并将每个元素放入其自己的行中。

首先,使用示例数据创建一个测试表:

CREATE OR REPLACE TEMPORARY TABLE table_name (
  nested_colmn  VARIANT
)
AS
  SELECT PARSE_JSON($1) AS nested_colmn
    FROM VALUES
           ($$
[
  {
    "firstKey": "val1",
    "secondKey": 2555,
    "thirdKey": false,
    "fourthkey": "otrvalue"
  },
  {
    "firstKey": "val2",
    "secondKey": 255221,
    "thirdKey": true,
    "fourthkey": "otrvalu"
  }
]
$$)
;

因此,这是一个示例查询:

SELECT F.VALUE:"firstKey"::VARCHAR AS FIRST_KEY
  FROM table_name T
      ,LATERAL FLATTEN(nested_colmn) F
;

答案 1 :(得分:0)

您将需要在数组上进行横向展平,然后解析JSON:

WITH x as (
SELECT array_construct(parse_json('
  {
    "firstKey": "val1",
    "secondKey": 2555,
    "thirdKey": false,
    "fourthkey": "otrvalue"
  }'),parse_json('
  {
    "firstKey": "val2",
    "secondKey": 255221,
    "thirdKey": true,
    "fourthkey": "otrvalu"
  }')) as var)
SELECT p.value:firstKey::varchar
FROM x,
lateral flatten(input => x.var) p;