检查记录字段是否包含使用标准sql的值(Google BigQuery)

时间:2017-03-24 16:27:21

标签: sql google-bigquery

例如,如果我有这样的架构:

[{
    "name": "fullName",
    "type": "string",
    "mode": "required"
 },
 {
    "name": "children",
    "type": "record",
    "mode": "repeated",
    "fields": [
    {
        "name": "name",
        "type": "string",
        "mode": "nullable"
    },
    {
        "name": "gender",
        "type": "string",
        "mode": "nullable"
    },
    {
        "name": "age",
        "type": "integer",
        "mode": "nullable"
    }] 
}]

一个人可以有多个孩子,孩子存储在重复的记录中,可以通过children.name,children.gender和children.age访问。 如果此人有多个子项,则可能会为此人显示多行,但所有这些行共享一个行号。

我想从这个表中选择一行中某个子名称与某个值匹配的所有行,我想它可能就像children.name包含的那样。

1 个答案:

答案 0 :(得分:2)

  

我将您的问题视为 - 如何选择所有有特定姓名的孩子的父母?

  
#standardSQL
SELECT *
FROM yourTable
WHERE 'child1' IN (SELECT name FROM UNNEST(children))

您可以使用虚拟数据进行上述测试,如下所示

#standardSQL
WITH yourTable AS (
  SELECT 
    'parentA' AS fullName, 
    [STRUCT<name STRING, gender STRING, age INT64>
      ('child1', 'm', 10), ('child2', 'f', 11), ('child3', 'f', 8)
    ] AS children UNION ALL
  SELECT 'parentB', 
    [STRUCT<name STRING, gender STRING, age INT64>
      ('child4', 'm', 10), ('child5', 'f', 11), ('child6', 'f', 8)] UNION ALL
  SELECT 'parentC', 
    [STRUCT<name STRING, gender STRING, age INT64>
      ('child7', 'm', 10), ('child1', 'f', 11), ('child8', 'f', 8)] 
)
SELECT *
FROM yourTable
WHERE 'child1' IN (SELECT name FROM UNNEST(children))