Chapter 3
Chapter 3
functions
P O S T G R E S Q L S U M M A R Y S TAT S A N D W I N D O W F U N C T I O N S
Michel Semaan
Data Scientist
Source table
Query Result
Result Result
Michel Semaan
Data Scientist
Motivation
LAST_VALUE
LAST_VALUE(City) OVER (
ORDER BY Year ASC
RANGE BETWEEN
UNBOUNDED PRECEDING AND
UNBOUNDED FOLLOWING
) AS Last_City
Without the frame, LAST_VALUE would return the row's value in the City column
By default, a frame starts at the beginning of a table or partition and ends at the current
row
Examples
Michel Semaan
Moving averages
Overview
Moving average (MA): Average of last n periods
Example: 10-day MA of units sold in sales is the average of the last 10 days' sold units
Used to indicate performance; if the sum is going down, overall performance is going
down
Table