在 Google bigquery 中加入 3 个表

Posted

技术标签:

【中文标题】在 Google bigquery 中加入 3 个表【英文标题】:Joining 3 tables in Google bigquery 【发布时间】:2012-09-08 18:33:02 【问题描述】:

下面的示例在第一个 JOIN 处停止并显示错误消息

在第 13 行第 4 列遇到“"JOIN" "JOIN ""。期待:")"

Bigquery 中的多个联接是否遗漏了一些明显的东西?

SELECT type.CourseType AS CourseType, 
       SUM(joined.assign.StudentCount) AS StudentN
FROM
  (
   SELECT assign.StateCourseCode,
          assign.StateCourseName,
          assign.MatchType, 
          assign.Term, 
          assign.StudentCount

   FROM [Assignment.AssignmentExtract5] AS assign

   JOIN SELECT wgt.Term,
               wgt.Weight 

    FROM [Crosswalk.TermWeights] AS wgt
     ON wgt.Term = assign.Term

 ) AS joined

JOIN SELECT type.CourseCode,
            type.CourseDescription, 
            type.CourseType, 
            type.CourseCategory 

FROM [Crosswalk.CourseTypeDescription] AS type
  ON joined.assign.StateCourseCode = type.CourseCode

GROUP BY CourseType

【问题讨论】:

【参考方案1】:

感谢 Ryan,非常感谢您的帮助。对于任何可能感兴趣的人,这里有一个有效的查询。

SELECT type.CourseCategory AS CourseCategory, 
       SUM(joined.assign.StudentCount) AS StudentN
    FROM
      (
       SELECT assign.StateCourseCode,
              assign.StateCourseName,
              assign.MatchType, 
              assign.Term, 
              assign.StudentCount

       FROM [Assignment.AssignmentExtract5] AS assign

       JOIN (SELECT Term,
                    Weight 

       FROM [Crosswalk.TermWeights]) AS wgt
       ON wgt.Term = assign.Term

     ) AS joined

    JOIN (SELECT CourseCode,
                 CourseDescription, 
                 CourseType, 
                 CourseCategory 

      FROM [Crosswalk.CourseTypeDescription]) AS type
    ON (joined.assign.StateCourseCode = type.CourseCode)

    GROUP BY CourseCategory;

【讨论】:

【参考方案2】:

我认为您只是在第 13 行缺少一个括号。

这个:

   JOIN SELECT wgt.Term,
               wgt.Weight 

    FROM [Crosswalk.TermWeights] AS wgt
     ON wgt.Term = assign.Term

应该是:

   JOIN (SELECT wgt.Term,
               wgt.Weight 

    FROM [Crosswalk.TermWeights]) AS wgt
     ON wgt.Term = assign.Term

更多信息: https://developers.google.com/bigquery/docs/query-reference#multiplejoinsexample

仅供参考 - JOIN 还没有我们想要的那么快。我们正在努力提高性能。

【讨论】:

以上是关于在 Google bigquery 中加入 3 个表的主要内容,如果未能解决你的问题,请参考以下文章

在 BigQuery 中加入 2 列结构

在BigQuery文件中加载列名称加载python

如何在 Google BigQuery 中加载大文本文件

Google BigQuery:当我从存储中加载数据时出现“string_field_0”

php 在Google日历中加入产品选项

在 Google 表格中加入条件转置