【问题标题】:How to return an array of structs from a struct of arrays in Standard SQL?如何从标准 SQL 中的数组结构返回结构数组?
【发布时间】:2019-10-04 18:41:56
【问题描述】:

我的表中有一个非重复记录列,我想访问它。 在这条记录上,有几个重复的值。

所以它是RECORD,就像这样:

STRUCT<item ARRAY<STRING> unit_cost ARRAY<INT64> quantity ARRAY<INT64>> as costs

例如。数据可能代表:

item ['cheese', 'ham', 'salad']
unit_cost [2, 5, 8]
quantity [1, 2, 1]

所以我想把它作为一个更好的数据结构,一个结构数组返回:

[
  {'item': 'cheese', 'unit_cost': 2, 'quantity': 1}, 
  {'item': 'ham', 'unit_cost': 5, 'quantity': 2}
  {'item': 'salad', 'unit_cost': 8, 'quantity': 1}
]

我试过了:

SELECT ARRAY_AGG(costs)

但它会导致

            [
              {
                "item": ['cheese', 'ham', 'salad'],
                "unit_cost": [2, 5, 8],
                "quantity": [1, 2, 1]
              }
            ]

这不是我期望它返回的结果。

是否可以在这里巧妙地使用标准 SQL 从多个 ARRAY 中的 STRUCT 到多个 STRUCT 中的 ARRAY

【问题讨论】:

    标签: struct google-bigquery array-agg


    【解决方案1】:

    以下是 BigQuery 标准 SQL

    #standardSQL
    SELECT 
      ARRAY(
        SELECT AS STRUCT item, unit_cost, quantity
        FROM UNNEST(costs.item) item WITH OFFSET
        LEFT JOIN UNNEST(costs.unit_cost) unit_cost WITH OFFSET USING(OFFSET)
        LEFT JOIN UNNEST(costs.quantity) quantity WITH OFFSET USING(OFFSET)
      ) AS costs
    FROM `project.dataset.table`   
    

    如果适用于您问题中的示例数据 - 结果是(在 JSON 视图中)

    [
      {
        "costs": [
          {
            "item": "cheese",
            "unit_cost": "2",
            "quantity": "1"
          },
          {
            "item": "ham",
            "unit_cost": "5",
            "quantity": "2"
          },
          {
            "item": "salad",
            "unit_cost": "8",
            "quantity": "1"
          }
        ]
      }
    ]
    

    【讨论】:

      【解决方案2】:

      您可以使用以下查询:

      with data as (
      select STRUCT<item ARRAY<STRING>, unit_cost ARRAY<INT64>, quantity ARRAY<INT64>>
        (['cheese', 'ham', 'salad'], [2, 5, 8], [1, 2, 1]) entry
      union all
      select (['othercheese', 'otherham', 'othersalad'], [3, 8, 10], [11, 22, 11])
      union all
      select (['othercheese', 'otherham', 'othersalad'], [3, 8, 10], [11, 22, 11])
      ) 
      SELECT ARRAY_AGG(STRUCT(item, unit_cost, quantity))
      FROM data, UNNEST(entry.item) item WITH OFFSET
          LEFT JOIN UNNEST(entry.unit_cost) unit_cost WITH OFFSET USING(OFFSET)
          LEFT JOIN UNNEST(entry.quantity) quantity WITH OFFSET USING(OFFSET)
      

      输出

      [
        {
          "f0_": [
            {
              "item": "cheese",
              "unit_cost": "2",
              "quantity": "1"
            },
            {
              "item": "ham",
              "unit_cost": "5",
              "quantity": "2"
            },
            {
              "item": "salad",
              "unit_cost": "8",
              "quantity": "1"
            },
            {
              "item": "othercheese",
              "unit_cost": "3",
              "quantity": "11"
            },
            {
              "item": "otherham",
              "unit_cost": "8",
              "quantity": "22"
            },
            {
              "item": "othersalad",
              "unit_cost": "10",
              "quantity": "11"
            },
            {
              "item": "othercheese",
              "unit_cost": "3",
              "quantity": "11"
            },
            {
              "item": "otherham",
              "unit_cost": "8",
              "quantity": "22"
            },
            {
              "item": "othersalad",
              "unit_cost": "10",
              "quantity": "11"
            }
          ]
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-06
        • 2013-08-02
        • 2022-10-20
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 1970-01-01
        相关资源
        最近更新 更多