Snowflake中具有多个JSON对象的横向扁平数组

时间:2019-03-20 00:58:56

标签: arrays json flatten snowflake-datawarehouse

我有一个包含多个JSON对象的数组。位于表中的任何JSON数组中的最大元素数为8。

这是数组原始值的示例:

                              variants
----------------------------------------------------------------

[
      {
        "id": 12388362846279,
        "inventory_quantity": 10,
        "sku": “sku1”
      },
      {
        "id": 12388391387207,
        "inventory_quantity": 31,
        "sku": “sku2”
      },
      {
        "id": 12394420142151,
        "inventory_quantity": 12,
        "sku": “sku3”
      },
      {
        "id": 12394426007623,
        "inventory_quantity": 4,
        "sku": “sku4”
      },
      {
        "id": 12394429022279,
        "inventory_quantity": 9,
        "sku": “sku5”
      },
      {
        "id": 12394431414343,
        "inventory_quantity": 15,
        "sku": “sku6”
      },
      {
        "id": 12394455597127,
        "inventory_quantity": 22,
        "sku": “sku7”
      },
      {
        "id": 12394459856967,
        "inventory_quantity": 0,
        "sku": “sku8”
      }
    ]

我的查询尝试展平并解析数组以为每个对象返回一行:

select 
      variants[0]:sku,
      variants[0]:inventory_quantity,
      variants[1]:sku,
      variants[1]:inventory_quantity,
      variants[2]:sku,
      variants[2]:inventory_quantity,
      variants[3]:sku,
      variants[3]:inventory_quantity,
      variants[4]:sku,
      variants[4]:inventory_quantity,
      variants[5]:sku,
      variants[5]:inventory_quantity,
      variants[6]:sku,
      variants[6]:inventory_quantity,
      variants[7]:sku,
      variants[7]:inventory_quantity
from table
, lateral flatten(input => variants)

但是,我的输出返回重复/重复的值:

+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+
| sku1 | 10 | sku2 | 31 | sku3 | 12 | sku4 | 4 | sku5 | 9 | sku6 | 15 | sku7 | 22 | sku8 | 0 |
+------+----+------+----+------+----+------+---+------+---+------+----+------+----+------+---+

我希望输出类似于以下内容:

+------+----+
| sku1 | 10 |
+------+----+
| sku2 | 31 |
+------+----+
| sku3 | 12 |
+------+----+
| sku4 | 4  |
+------+----+
| sku5 | 9  |
+------+----+
| sku6 | 15 |
+------+----+
| sku7 | 22 |
+------+----+
| sku8 | 0  |
+------+----+

1 个答案:

答案 0 :(得分:2)

使用LATERAL FLATTEN不需要显式引用数组位置。数组的每个成员成为其自己的行。因此,要获得上面想要的结果,只需使用:

select v.value:sku::varchar, 
       v.value:inventory_quantity 
from table, 
lateral flatten(input => table.variants) v
;

如果表中的某些列在每行中要引用的数组之外,只需将它们包括在SELECT中。本质上,从数组中展平的行被隐式“连接”到表的非嵌套列...