PostgreSQL Summary Stats and Window Functions
Michel Semaan
Data Scientist
USA total and running total of Summer Olympics gold medals since 2004
| Year | Medals | Medals_RT |
|------|--------|-----------|
| 2004 | 116 | 116 |
| 2008 | 125 | 241 |
| 2012 | 147 | 388 |
Discus throw reigning champion status
| Year | Champion | Last_Champion | Reigning_Champion |
|------|----------|---------------|-------------------|
| 1996 | GER | null | false |
| 2000 | LTU | GER | false |
| 2004 | LTU | LTU | true |
| 2008 | EST | LTU | false |
| 2012 | GER | EST | false |
Year
, City
Sport
, Discipline
, Event
Athlete
, Country
, Gender
Medal
GROUP BY
aggregate functions, but all rows remain in the outputUses
Query
SELECT
Year, Event, Country
FROM Summer_Medals
WHERE
Medal = 'Gold';
Result
| Year | Event | Country |
|------|----------------------------|---------|
| 1896 | 100M Freestyle | HUN |
| 1896 | 100M Freestyle For Sailors | GRE |
| 1896 | 1200M Freestyle | HUN |
| ... | ... | ... |
Query
SELECT
Year, Event, Country,
ROW_NUMBER() OVER () AS Row_N
FROM Summer_Medals
WHERE
Medal = 'Gold';
Result
| Year | Event | Country | Row_N |
|------|----------------------------|---------|-------|
| 1896 | 100M Freestyle | HUN | 1 |
| 1896 | 100M Freestyle For Sailors | GRE | 2 |
| 1896 | 1200M Freestyle | HUN | 3 |
| ... | ... | ... | ... |
Query
SELECT
Year, Event, Country,
ROW_NUMBER() OVER () AS Row_N
FROM Summer_Medals
WHERE
Medal = 'Gold';
FUNCTION_NAME() OVER (...)
ORDER BY
PARTITION BY
ROWS/RANGE PRECEDING/FOLLOWING/UNBOUNDED
PostgreSQL Summary Stats and Window Functions