BigQuery RATIO_TO_REPORT用于所有数据无分区

时间:2017-07-29 04:09:01

标签: sql google-bigquery google-cloud-platform

我想计算指定字段的比例,我知道在遗留sql中我可以使用RATIO_TO_REPORT函数ex:

SELECT
  month,
  RATIO_TO_REPORT(totalPoint) over (partition by month)
FROM (
  SELECT
    format_datetime('%Y-%m', ts) AS month,
    SUM(point) AS totalPoint
  FROM
    `userPurchase`
  GROUP BY
    month
  ORDER BY
    month )

但我希望得到的比率是按照所有数据计算的,没有分区,例如:(此代码不起作用)

SELECT
  month,
  RATIO_TO_REPORT(totalPoint) over (partition by "all"),
# RATIO_TO_REPORT(totalPoint) over (partition by null)
FROM (
  SELECT
    format_datetime('%Y-%m', ts) AS month,
    SUM(point) AS totalPoint
  FROM
    `userPurchase`
  GROUP BY
    month
  ORDER BY
    month )

它不起作用,我怎么能做同样的事情?谢谢!

1 个答案:

答案 0 :(得分:1)

假设其余代码是正确的 - 只需省略partition by部分

RATIO_TO_REPORT(totalPoint) OVER ()