错误范围和未范围的bigquery问题

时间:2016-05-10 19:03:22

标签: google-bigquery

SELECT
MAX(IF(customDimensions.index=1, customDimensions.value, NULL)) WITHIN hits 
AS CustomerID,
date as date
SUM(totals.visits) Visits,
EXACT_COUNT_DISTINCT(fullvisitorid) AS Users,
FROM [xxxxxxx.ga_sessions_xxxxx] 
Group by CustomerID;

当我运行上述查询时出现以下错误“错误:作用域和未作用域聚合函数的无效混合”。有人可以告诉我如何解决这个问题吗?

由于

1 个答案:

答案 0 :(得分:4)

使用standard SQL(取消选中"使用旧版SQL"框"显示选项"),您应该能够构建一个查询,例如:

SELECT
  (SELECT MAX(IF(index=1, value, NULL))
   FROM UNNEST(customDimensions)) AS CustomerID,
  date as date,
  SUM(totals.visits) AS Visits,
  COUNT(DISTINCT fullvisitorid) AS Users
FROM `xxxxxxx.ga_sessions_xxxxx`
GROUP BY CustomerID, date;

以下是一个示例查询来说明该方法:

WITH SampleTable AS (
  SELECT ARRAY<STRUCT<index INT64, value STRING>>
      [(5, "foo"), (1, "bar"), (1, "baz")] AS customDimensions,
    "20160518" AS date,
    STRUCT(10 AS visits) AS totals,
    "visitorid" AS fullvisitorid
  UNION ALL SELECT ARRAY<STRUCT<index INT64, value STRING>>
      [(0, "foo"), (4, "bar"), (2, "baz")] AS customDimensions,
    "20160519" AS date,
    STRUCT(20 AS visits) AS totals,
    "visitorid" AS fullvisitorid)
SELECT
  (SELECT MAX(IF(index=1, value, NULL))
   FROM UNNEST(customDimensions)) AS CustomerID,
  date as date,
  SUM(totals.visits) AS Visits,
  COUNT(DISTINCT fullvisitorid) AS Users
FROM SampleTable
GROUP BY CustomerID, date;
+------------+----------+--------+-------+
| CustomerID |   date   | Visits | Users |
+------------+----------+--------+-------+
| baz        | 20160518 |     10 |     1 |
| NULL       | 20160519 |     20 |     1 |
+------------+----------+--------+-------+

或者,我认为您需要一个子查询来使用旧SQL计算customDimensions重复字段的最大值:

SELECT
  CustomerID,
  date as date,
  SUM(totals.visits) Visits,
  EXACT_COUNT_DISTINCT(fullvisitorid) AS Users
FROM (
  SELECT
    *,
    MAX(IF(customDimensions.index=1, customDimensions.value, NULL))
      WITHIN customDimensions AS CustomerID
  FROM [xxxxxxx.ga_sessions_xxxxx])
GROUP BY CustomerID, date;