是否可以计算BigQuery中的重叠字段的累计总数?

时间:2016-08-12 16:03:03

标签: google-bigquery

有一个类似的问题(Is BigQuery ROLLUP supports grouping by repeated fields),但它没有一个例子。

请考虑以下代码:

SELECT    user_segments AS user_segments,
SUM(impressions) AS imps,
SUM(clicks) AS clicks,
FROM [theTable] 
GROUP BY ROLLUP (user_segments)
ORDER BY imps DESC
LIMIT 1000

其中theTable包含两个用户的展示次数和点击次数(表格只有10行,而每行的展示次数= 1):

{"impressions": 1, "user_segments": [0, 1], "user_id": "A0", "clicks": 0}
{"impressions": 1, "user_segments": [1, 2], "user_id": "A1", "clicks": 1}
{"impressions": 1, "user_segments": [0, 1], "user_id": "A0", "clicks": 2}
{"impressions": 1, "user_segments": [1, 2], "user_id": "A1", "clicks": 0}
{"impressions": 1, "user_segments": [0, 1], "user_id": "A0", "clicks": 1}
{"impressions": 1, "user_segments": [1, 2], "user_id": "A1", "clicks": 2}
{"impressions": 1, "user_segments": [0, 1], "user_id": "A0", "clicks": 0}
{"impressions": 1, "user_segments": [1, 2], "user_id": "A1", "clicks": 1}
{"impressions": 1, "user_segments": [0, 1], "user_id": "A0", "clicks": 2}
{"impressions": 1, "user_segments": [1, 2], "user_id": "A1", "clicks": 0}

查询输出是:

user_segments imps clicks
null          20   18
1             10   9
2             5    4
0             5    5

但表格中只有10个(10个!)的展示次数。在我看来,总数的正确值将是:

user_segments imps clicks
null          10   9
1             10   9
2             5    4
0             5    5

有没有办法获得没有单独查询的正确总数?谢谢!

1 个答案:

答案 0 :(得分:1)

下面的

是你最有可能已经使用过的明显的解决方法 - 但仍然只是以及以后发布

SELECT * FROM (
  SELECT 
    user_segments AS user_segments,
    SUM(impressions) AS imps,
    SUM(clicks) AS clicks
  FROM theTable
  GROUP BY user_segments
), (
  SELECT 
    NULL AS user_segments,
    SUM(impressions) AS imps,
    SUM(clicks) AS clicks
  FROM theTable
)
ORDER BY imps DESC, user_segments