Visualize a rolling average 3 days
06/01/2022: 2 = 2.0
06/02/2022: 2 + 1 = 3 / 2 days = 1.5
06/03/2022: 2 + 1 + 3 = 6 / 3 days = 2.0
06/04/2022: 2 + 1 + 3 + 4 = 10 / 4 days = 2.5
06/05/2022: 2 + 1 + 3 + 4 + 5 = 15 / 5 days = 3.0
What does ROW_NUMBER() do?
Assigns a unique sequential number to each row within a partition
What does RANK() do?
Assigns same rank to ties but skips numbers after ties
What does DENSE_RANK() do?
Assigns same rank to ties with no gaps/skips
What does LAG() do?
Returns previous row value
What does LEAD() do?
Returns next row value
What does SUM() OVER() do?
Calculates running total without collasping the rows like GROUP BY
What does AVG() OVER() do?
Calculates moving average across rows
What does PARTITION BY do?
Splits data into groups without collapsing rows
What does ORDER BY do in window functions?
Defines row order for calculations
What does ROWS BETWEEN mean?
Defines which rows are included in the window
What does UNBOUNDED PRECEDING mean?
Start from first row of partition
What does CURRENT ROW mean?
Includes the current row as boundary
What does UNBOUNDED FOLLOWING mean?
Includes all rows after current row
What does ROWS BETWEEN 2 PRECEDING AND CURRENT ROW mean?
Includes current row + 2 previous rows
What does ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING mean?
Includes previous current and next row
What does ROWS mean?
Counts physical rows
What does RANGE mean?
Uses value-based range not row count
How to get latest row per user?
Use ROW_NUMBER with PARTITION and filter rn=1
How to calculate MoM change?
Use LAG to compare current vs previous row
How to get top N including ties?
Use DENSE_RANK and filter rank <= N
How to compute running total?
Use SUM OVER with ORDER BY
How to compute moving average?
Use AVG OVER with ROWS BETWEEN
Visualize ROW_NUMBER() ?
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date DESC)