分组和旋转自定义维度

时间:2016-11-16 22:09:03

标签: google-bigquery

我在4个国家/地区为4个网站的客户工作。我们的一些博客文章已在4网站上翻译和发布。所以,我发现每个帖子都有一个唯一的ID,可以访问四个不同的国家。

我希望能够查询每个国家/地区的每个帖子(因此每个帖子ID)的访问次数。

我运行了以下查询,但它不起作用:

SELECT * FROM (
SELECT hits.customDimensions.value AS postid
FROM [storied-toolbox-115472:1127229124.ga_sessions_20161108]
WHERE hits.customDimensions.index=10), (SELECT hits.customDimensions.value AS Country,
count(totals.visits) as visit,
FROM [storied-toolbox-115472:1127229124.ga_sessions_20161108]
WHERE hits.customDimensions.index=1
group by
Country)

查询运行正确,但结果是我有帖子ID,国家被引用为null。如果我有国家,则将post id引用为null。

2 个答案:

答案 0 :(得分:2)

我可能误解了这个问题,但这里有一个查询,它应该为您提供所有postid s(作为数组)以及每个国家/地区的访问总和。您需要启用standard SQL才能运行它。

SELECT
  ARRAY_AGG((SELECT value FROM UNNEST(hits.customDimensions)
             WHERE index = 10)) AS postids,
  (SELECT value FROM UNNEST(hits.customDimensions) WHERE index = 1) AS Country,
  SUM(totals.visits) AS visits
FROM `storied-toolbox-115472.1127229124.ga_sessions_20161108` AS t
  CROSS JOIN UNNEST(hits) AS hits
WHERE EXISTS (SELECT value FROM UNNEST(hits.customDimensions)
              WHERE index = 10)
GROUP BY Country;

答案 1 :(得分:1)

没有机会进行任何测试,所以下面主要是“盲目”拍摄,但我通常擅长这一点。如果这是错误的,请不要判断:o)

SELECT
  postid,
  country,
  SUM(visits) AS visits
FROM (
  SELECT 
    visitid, 
    hits.customDimensions.value AS postid
  FROM [storied-toolbox-115472:1127229124.ga_sessions_20161108]
  WHERE hits.customDimensions.index = 10
) AS posts
JOIN (
  SELECT 
    visitid, 
    hits.customDimensions.value AS country,
    SUM(totals.visits) AS visits
  FROM [storied-toolbox-115472:1127229124.ga_sessions_20161108]
  WHERE hits.customDimensions.index = 1
  GROUP BY visitid, country
) AS stats
ON posts.visitid = stats.visitid 
GROUP BY postid, country