包含2个重复字段的查询表

时间:2018-01-22 07:00:17

标签: google-bigquery standard-sql

我有一个包含多个重复字段的模式,这些字段都是记录类型。例如,架构是这样的:

name: string,

class: record(repeated),

class.id :string

class.title: string,

school: record(repeated),

school.id: string,

school.title: string.

如何在标准SQL中查询并返回带有班级和学校列表的学生行(班级和学校完全无关)。

实际上,一个简单的SELECT *可以工作,但我想选择每一列并重命名它们,即SELECT name作为student_name,classes,schools。我还想过滤一些类和学校,即只显示满足某些约束的类。

示例:name:'Tom',class:[{id:1,title:math},{id:2,title:physics},{id:3,title:math}],school:[{id :1,title:Yale},{id:2,title:Havard},{id:3,title:Yale}] 我想通过class.title和school.title过滤返回如下行:name:'Tom',class:[{id:1,title:math},{id:3,title:math}],school :[{id:1,title:Yale},{id:3,title:Yale}]

1 个答案:

答案 0 :(得分:0)

以下是BigQuery Standard SQL

   
#standardSQL
SELECT name as student_name, 
  ARRAY(SELECT AS STRUCT * FROM UNNEST(class) class WHERE class.title = 'math') classes,
  ARRAY(SELECT AS STRUCT * FROM UNNEST(school) school WHERE school.title = 'Yale') schools
FROM `yourproject.yourdataset.yourtable`  

您可以使用问题中的虚拟数据来测试/播放上面的内容:

#standardSQL
WITH `yourproject.yourdataset.yourtable` AS (
  SELECT 
    'Tom' name, 
    [
      STRUCT(1 AS id, 'math' AS title), 
      (2, 'physics'), 
      (3, 'math')
    ] class, 
    [
      STRUCT(1 AS id, 'Yale' AS title), 
      (2, 'Havard'), 
      (3, 'Yale')
    ] school 
)
SELECT name as student_name, 
  ARRAY(SELECT AS STRUCT * FROM UNNEST(class) class WHERE class.title = 'math') classes,
  ARRAY(SELECT AS STRUCT * FROM UNNEST(school) school WHERE school.title = 'Yale') schools
FROM `yourproject.yourdataset.yourtable`   

结果如下

name    class.id    class.title school.id   school.title     
Tom     1           math        1           Yale     
        3           math        3           Yale     

上面应该让您了解如何在重复字段内过滤