使用带有展平自定义维度的where语句

时间:2016-11-09 19:09:55

标签: google-bigquery

我构建了以下查询:

SELECT
  MAX(IF (hits.customDimensions.index = 10, hits.customDimensions.value, NULL)) WITHIN RECORD AS postId,
  DATE(MAX(IF (hits.customDimensions.index = 4, hits.customDimensions.value, NULL))) WITHIN RECORD AS Datepublished,
  MAX(IF (hits.customDimensions.index = 1, hits.customDimensions.value, NULL)) WITHIN RECORD AS Country,
  MAX(IF (hits.customDimensions.index = 7, hits.customDimensions.value, NULL)) WITHIN RECORD AS Author,
FROM
  (TABLE_DATE_RANGE([storied-toolbox-145015:102152044.ga_sessions_], DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY'), DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY')))

但是我希望能够使用基于其中一个自定义维度的where语句。像这样where Country = 'fr'。我尝试了各种解决方案但是我总是遇到Over子句错误或范围错误。

由于我想在Google Data Studio中使用此查询,因此我需要使用SQL遗留构建它。

谢谢!

1 个答案:

答案 0 :(得分:1)

  

...我希望能够使用基于其中一个自定义维度的where语句

下面假设您的查询按预期工作,因此它只解决"过滤"按国家来说这基本上就是问题!

SELECT
  MAX(IF (hits.customDimensions.index = 10, hits.customDimensions.value, NULL)) WITHIN RECORD AS postId,
  DATE(MAX(IF (hits.customDimensions.index = 4, hits.customDimensions.value, NULL))) WITHIN RECORD AS Datepublished,
  MAX(IF (hits.customDimensions.index = 1, hits.customDimensions.value, NULL)) WITHIN RECORD AS Country,
  MAX(IF (hits.customDimensions.index = 7, hits.customDimensions.value, NULL)) WITHIN RECORD AS Author,
FROM
  (TABLE_DATE_RANGE([storied-toolbox-145015:102152044.ga_sessions_], DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY'), DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY')))
HAVING Country = 'fr'  

你可以看到"技巧"正在使用HAVING而不是WHERE