You are on page 1of 2

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.

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW -- default


ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING

RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING


RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING -- subtracts 2 and adds 2 to the current order key value

The analytical functions are the following:

• Navigation Functions: FIRST_VALUE, LAST_VALUE, NTH_VALUE, LEAD,


LAG
o Navigation functions are based on the ordering of rows in the partition
• Numbering Functions: RANK, DENSE_RANK, ROW_NUMBER
o Numbering functions are based on the ordering of rows in the partition
• Aggregate Functions: ANY_VALUE, ARRAY_AGG, ARRAY_CONCAT_AGG,
AVG, BIT_AND, BIT_OR, BIT_XOR, COUNT, COUNTIF, LOGICAL_AND,
LOGICAL_OR, MAX, MIN, STRING_AGG, SUM
o Aggregate functions are based on the ordering of rows and optionally on the
value of the column specified

In Oracle and Spark:

• 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

You might also like