【问题标题】:access array in sql google BigQuery在 sql google BigQuery 中访问数组
【发布时间】:2019-10-20 22:50:16
【问题描述】:

目前,我的表的一个属性中有一组对象

table metadata{
    String id,
    repeated Point points
}

table Point{
    String x,
    String y
}

当我这样做时

select id, points from metadata

在 Google BigQuery 中

我得到格式的数据

[
  {
    "id": "453ee599-0e74-4098-bda5-9808953cf757",
    "points": [
      {
        "x": "x_",
        "y": "y1_"
      },
      {
        "x": "x2_",
        "y": "y2_",
      }
    ]
  }
]

我应该如何修改我的 sql 查询以使结果符合格式

[
  {
    "id": "453ee599-0e74-4098-bda5-9808953cf757",
    "x" : "x1_",
    "y" : "y1_",
  },
  {
    "id": "453ee599-0e74-4098-bda5-9808953cf757",
    "x" : "x2_",
    "y" : "y2_",
  }
]

【问题讨论】:

  • 我阅读了您的帖子,但无法理解您的数据结构。你能提供一个 dbfiddle 的例子吗?
  • @user3285099 - 考虑对有帮助的答案进行投票,并接受您认为最有帮助的答案!

标签: mysql sql google-bigquery


【解决方案1】:
#standardSQL
SELECT id, point.* 
FROM `project.dataset.metadata`, 
UNNEST(points) point

如果适用于您问题的样本数据 -

结果是

Row id                                      x   y    
1   453ee599-0e74-4098-bda5-9808953cf757    x_  y_   
2   453ee599-0e74-4098-bda5-9808953cf757    x2_ y2_  

或者如果存在于 JSON 中

[
  {
    "id": "453ee599-0e74-4098-bda5-9808953cf757",
    "x": "x_",
    "y": "y_"
  },
  {
    "id": "453ee599-0e74-4098-bda5-9808953cf757",
    "x": "x2_",
    "y": "y2_"
  }
]

【讨论】:

    【解决方案2】:

    您需要取消嵌套:

    select id, point
    from metadata,
    unnest(points) as point
    

    working with arrays documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 2020-12-09
      • 1970-01-01
      • 2017-06-30
      • 2021-09-27
      • 2016-09-14
      相关资源
      最近更新 更多