Skip to content

Instantly share code, notes, and snippets.

@romain9292
Last active August 12, 2022 16:36
Show Gist options
  • Select an option

  • Save romain9292/573c77d04a279dc97d60b0af9cdd3a50 to your computer and use it in GitHub Desktop.

Select an option

Save romain9292/573c77d04a279dc97d60b0af9cdd3a50 to your computer and use it in GitHub Desktop.
[Perc Total - Product categories - Part 2] How to compute a percentage of total in BigQuery using SQL #SQL #BigQuery
WITH
revenue_product AS (
SELECT
product_category,
ROUND(SUM(product_revenue),0) AS revenue_per_category
FROM
`datastic.variables.base_table`
GROUP BY
1)
-- Main Query
SELECT
*,
SUM(revenue_per_category) OVER() AS total_revenue,
SAFE_DIVIDE(revenue_per_category,SUM(revenue_per_category) OVER()) AS share_revenue
FROM
revenue_product
ORDER BY
share_revenue DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment