Summchpt 3
Summchpt 3
functions
POSTGRESQL SUMMARY STATS AND WINDOW FUNCTIONS
Michel Semaan
Data Scientist
Source table
Query Result
SELECT MAX(Medals) AS
Max_Medals FROM Brazil_Medals;
SELECT SUM(Medals) AS
Total_Medals FROM Brazil_Medals;
POSTGRESQL SUMMARY STATS AND WINDOW FUNCTIONS
MAX Window function
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
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 momentum/trends
Also useful in eliminating seasonality
Moving total: Sum of last n periods
Example: Sum of the last 3 Olympic games' medals
Used to indicate performance; if the sum is going down, overall performance is going
down
Table