SQL Analytic Functions
SQL Analytic Functions
If no PARTITION BY clause is present, BigQuery treats the entire input as a single partition.
If no ORDER BY clause is present, row ordering within a partition is non-deterministic.
ROWS-based window frames compute the window frame based on physical offsets from
the current row.
RANGE-based window frames compute the window frame based on a logical range of
rows around the current row based on the current row's ORDER BY key value. The
provided range value is added or subtracted to the current row's key value to define a
starting or ending range boundary for the window frame.
• If PARTITION BY is not specified, the function treats all rows of the query result set
as a single partition. The function will be applied on all rows in the partition if you
don't specify the ORDER BY clause.
• If it is specified (ORDER BY), and a ROWS/RANGE is not specified, then default
RANGE UNBOUNDED PRECEDING AND CURRENT ROW is used as default
for window frame by the functions that can accept optional ROWS/RANGE
specification (for example min or max).
When you use GROUP BY and WINDOW functions, the GROUP BY is first applied and
then WINDOW functions.
That's why you can calculate the percentage on groups (when using GROUP BY) like this:
SELECT
sex, employed,
COUNT(*) / CAST( SUM(count(*)) over (partition by sex) as float)
FROM my_table
GROUP BY 1, 2
SELECT
PersonID,
SUM(PA.Total),
SUM(PA.Total) * 100.0 / SUM(SUM(PA.Total)) OVER () AS Percentage
FROM my_table
GROUP BY 1