横向平坦的阵列在雪花中带有多个JSON对象



我有一个带有多个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  |
+------+----+

使用横向平坦的

消除了您明确引用数组位置的需求。阵列的每个成员成为其自己的行。因此,要获取您想要的结果,只需使用:

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

如果从表中有一些列,这些列位于您要在每行中引用的数组之外,则只需将它们包含在Select中即可。本质上,来自数组的扁平行被"连接"到表的非巢列中,隐式...

相关内容

  • 没有找到相关文章

最新更新