在Google BigQuery中自定义维度获取值时出错

时间:2018-09-19 12:10:24

标签: sql google-bigquery custom-dimensions

我在Google大查询中遇到有关提取自定义维度的问题。 已有人问过这个问题,但是解决方案不起作用。

问题是,当我尝试提取像这样的自定义维度的信息

SELECT
fullvisitorId,
visitid,
hit.hitnumber,
(SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 1) as productCategory,
(SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 2) as loyaltyClass,
(SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 3) as existingCustomer
FROM [<id>.ga_sessions_20180805],UNNEST(hits) as hit
LIMIT 100

然后我得到一个错误“表名称“命中”无法解析:数据集名称丢失。”

我试图使用其他类似的解决方案

SELECT
    fullvisitorId,
    visitid,
    hit.hitnumber,
    (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 1) as productCategory,
    (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 2) as loyaltyClass,
    (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 3) as existingCustomer
FROM `<id>.ga_sessions_*`, UNNEST(hits) AS h
WHERE _TABLE_SUFFIX = '20180805'

然后我收到另一个错误无效的表名:<id>.ga_sessions_* [尝试使用标准SQL(https://cloud.google.com/bigquery/docs/reference/standard-sql/enabling-standard-sql)]

更新:我什至尝试了最基本的查询

    SELECT
      *
    FROM [<id>.ga_sessions_20180805]
    LEFT JOIN UNNEST(hits) as hits
   LIMIT 10

仍然返回相同的错误。...

我对两个脚本都犯了什么错误?以及如何获取自定义尺寸值?

非常感谢!

2 个答案:

答案 0 :(得分:3)

您可以使用所有情况都支持的情况

    SELECT
    fullvisitorId,
    visitid,
    h.hitnumber,
    case when x.index = 1 then x.value end as productCategory,
    case when x.index = 2 then x.value end as loyaltyClass,
    case when x.index = 3 then x.value end as existingCustomer
    FROM [<id>.ga_sessions_20180805]
    LEFT JOIN UNNEST( hits ) as h
   WHERE _TABLE_SUFFIX = '20180805'

注意:为查询启用标准SQL,或使用新的BigQuery UI

答案 1 :(得分:3)

在您的第一个查询中-您只需要在下面的行中修复表引用

FROM [<id>.ga_sessions_20180805],UNNEST(hits) as hit

类似于

FROM `yourproject.yourdataset.ga_sessions_20180805`,UNNEST(hits) as hit  

与第二个查询类似的解决方法,但除此之外-别名h应替换为hit,如下所示

FROM `yourproject.yourdataset.ga_sessions_*`, UNNEST(hits) AS hit

注意:上面是针对BigQuery Standard SQL的-因此您可以将第一行添加到查询的最下方

#standardSQL     

例如

#standardSQL     
SELECT
  fullvisitorId,
  visitid,
  hit.hitnumber,
  (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 1) as productCategory,
  (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 2) as loyaltyClass,
  (SELECT x.value FROM UNNEST(hit.customDimensions) x WHERE x.index = 3) as existingCustomer
FROM `yourproject.yourdataset.ga_sessions_20180805`,UNNEST(hits) as hit 
LIMIT 100