在同一个Big Query查询中查询聚合和分析聚合

时间:2017-01-23 14:32:42

标签: sql google-bigquery

使用Big Query,我正在尝试查询

count(distinct col ),count(distinct col )over(partition by col2
< / p>

但是我收到了一个意想不到的错误。

以下是我试图执行的查询模板:

SELECT
          country,
          partner,
          segment_id,
          COUNT(DISTINCT pv_id) pvs,
          COUNT(DISTINCT pv_id) over(PARTITION BY country) country_total_pvs
FROM (...)
GROUP BY
          country,
          partner,
          segment_id

我不断得到的错误:

  

错误:表达式&#39; pv_id&#39;在GROUP BY列表中不存在

没有第5列(分析计数),查询执行时没有任何错误。

思想?

非常感谢!

2 个答案:

答案 0 :(得分:1)

以下情况应该有效,但我猜不到你想做的事情:

SELECT country, partner, segment_id,
       COUNT(DISTINCT pv_id) pvs,
       SUM(COUNT(DISTINCT pv_id)) OVER (PARTITION BY country) as country_total_pvs
FROM (...) q
GROUP BY country, partner, segment_id;

相反:

SELECT country, partner, segment_id,
       COUNT(DISTINCT pv_id) pvs,
       country_total_pvs
FROM (SELECT q.*,
             COUNT(DISTINCT pv_id) OVER (PARTITION BY country) as country_total_pvs
      FROM (...) q
     ) q
GROUP BY country, partner, segment_id, country_total_pvs;

答案 1 :(得分:0)

尝试以下

SELECT
  a.country AS country,
  partner,
  segment_id,
  COUNT(DISTINCT pv_id) pvs,
  country_total_pvs
FROM youTable AS a
LEFT JOIN (
  SELECT country, COUNT(DISTINCT pv_id) country_total_pvs
  FROM youTable GROUP BY country
) AS b
ON a.country = b.country
GROUP BY
  country,
  partner,
  segment_id,
  country_total_pvs  

请记住 - 以上将提供&#34;正确&#34; BigQuery StandardSQL的不同计数,但在BigQuery Legacy SQL中COUNT(DISTINCT)是一种统计近似值,并不保证是精确的。您可以使用EXACT_COUNT_DISTINCT代替

以下是略微优化的版本

SELECT
  a.country AS country,
  partner,
  segment_id,
  pvs,
  country_total_pvs
FROM (
  SELECT
    country,
    partner,
    segment_id,
    COUNT(DISTINCT pv_id) pvs
  FROM youTable 
  GROUP BY country, partner, segment_id
) AS a
LEFT JOIN (
  SELECT country,
    COUNT(DISTINCT pv_id) country_total_pvs
  FROM youTable GROUP BY country
) AS b
ON a.country = b.country