无效的表格名称取消嵌套

时间:2019-08-14 12:20:54

标签: sql google-bigquery unnest

我正在学习在bigquery中进行嵌套,这是我的理解,UNNEST(hits)跟随表名,并在where子句中的WHERE子句中定义hits.type。下面的查询给出错误的无效表名,但是表名正确:

SELECT index, value
FROM (SELECT hits.customDimensions.index as index,
             hits.customDimensions.value as value
      FROM `60400612.ga_sessions_*`,
            UNNEST(hits) AS hits
      WHERE _table_suffix BETWEEN '20190714' and '20190811'
         AND hits.type = 'customDimensions')
GROUP BY 1,2

我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

我实际上不确定范围规则是什么。但是,我认为使用相同的名称是一种不好的做法。因此,只需将其重命名为hit

FROM 60400612.ga_sessions_* CROSS JOIN
     UNNEST(hits) AS hit
WHERE  _table_suffix BETWEEN '20190714' and '20190811' AND
       hit.type = 'customDimensions'

答案 1 :(得分:0)

例如,此查询确实有效(我使用的嵌套结构相同):

SELECT Source, Medium, New_Users, Sessions, ((Bounces/Sessions) * 100) AS bounce_rate
FROM(SELECT trafficSource.source AS Source, TrafficSource.medium AS medium, 
COUNT(DISTINCT fullVisitorId) AS Users, COUNT(DISTINCT(
      CASE
        WHEN totals.newVisits = 1 THEN fullVisitorId
      ELSE
      NULL
    END
      )) AS New_Users, 
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))) AS Sessions, sum(totals.bounces) AS Bounces
FROM
  `60400612.ga_sessions_*`,
  UNNEST(hits) AS hits
  WHERE 
  _table_suffix BETWEEN '20190714' and '20190811'
  AND hits.type = 'PAGE'
  AND hits.page.hostname like 'xxx'
GROUP BY 1,2)
ORDER BY 4 DESC