BigQuery - UNNEST 多维数组

BigQuery - UNNEST with a multidimensional array

我需要取消嵌套包含多个项目的多维数组。

我的数据如下所示(两行):

此 table 中的每个订单都有一个 fulfillments 列,它是一个多维数组。

fulfilments 有多个 line_items。有些订单只有有1个line_items,有些订单有多个line_items.

我的目标是从每个 line_items 中获取每个产品名称和数量,然后执行 运行 sum/tally.

这是 fulfillments 列数据的 JSON 导出:

"{
  "fulfillments": [{
    "value": {
      "id": "6575674745",
      "tracking_number": null,
      "line_items": [{
        "value": {
          "properties": [{
            "value": {
              "name": "shipping_interval_unit_type",
              "value": "Months"
            }
          }, {
            "value": {
              "name": "shipping_interval_frequency",
              "value": "1"
            }
          }, {
            "value": {
              "name": "ch_item_id",
              "value": "77321530"
            }
          }],
          "product_id": "4790985097351",
          "total_discount_set": {
            "shop_money": {
              "amount": "0.00",
              "currency_code": "USD"
            },
            "presentment_money": {
              "amount": "0.00",
              "currency_code": "USD"
            }
          },
          "sku": “10101”,
          "name": “Product1”,
          "variant_id": "33433213108359",
          "quantity": "1",
          "taxable": "true",
          "total_discount": "0",
          "title": "Product1 Title",
          "price_set": {
            "shop_money": {
              "amount": "12.00",
              "currency_code": "USD"
            },
            "presentment_money": {
              "amount": "12.00",
              "currency_code": "USD"
            }
          },
          "product_exists": "true"
        }
      }],
      "service": "manual",
      "tracking_urls": []
    }
  }, {
    "value": {
      "tracking_url": null,
      "id": "3555531128967",
      "tracking_company": null,
      "tracking_number": null,
      "name": "#3129472402.2",
      "tracking_numbers": [],
      "line_items": [{
        "value": {
          "properties": [{
            "value": {
              "name": "shipping_interval_frequency",
              "value": "1"
            }
          }, {
            "value": {
              "name": "shipping_interval_unit_type",
              "value": "Months"
            }
          }, {
            "value": {
              "name": "ch_item_id",
              "value": "77321529"
            }
          }],
          "product_id": "5216409780359",
          "total_discount_set": {
            "shop_money": {
              "amount": "0.00",
              "currency_code": "USD"
            },
            "presentment_money": {
              "amount": "0.00",
              "currency_code": "USD"
            }
          },
          "sku": "9005",
          "name": “Product2”,
          "quantity": "1",
          "title": “Product2 Title”,
          "price_set": {
            "shop_money": {
              "amount": "0.00",
              "currency_code": "USD"
            },
            "presentment_money": {
              "amount": "0.00",
              "currency_code": "USD"
            }
          },
          "product_exists": "true"
        }
      }],
      "service": "manual",
      "tracking_urls": []
    }
  }]
}"
SELECT
  fulfillments[ safe_OFFSET (0)].value.line_items[ safe_OFFSET (0)].value.name AS name,
  fulfillments[ safe_OFFSET (0)].value.line_items[ safe_OFFSET (0)].value.quantity AS quantity
FROM
  `mydatabase`

如果订单在 line_items

中有多个数组,则不理想

当我不知道每个数组有多大时如何取消嵌套多维数组?

SELECT
*
FROM
  `mydatabase`,
  UNNEST(fulfillments) as a

只给我另一个要展平的数组

鉴于您的情况,您可以尝试使用此查询来解除 line_items 与产品名称和数量的嵌套:

SELECT 
    line_items.value.name,
    line_items.value.quantity,
    line_items.value.product_id

from `mydatabase`, 
    unnest(fulfillments) fulfillments,
    unnest(fulfillments.value.line_items) line_items

输出: