summchpt4
summchpt4
Michel Semaan
Data Scientist
Transforming tables
Before After
SELECT
Country, Year,
RANK() OVER
(PARTITION BY Year ORDER BY Awards DESC) :: INTEGER
AS rank
FROM Country_Awards
ORDER BY Country ASC, Year ASC;
Michel Semaan
Data Scientist
Group-level totals
Chinese and Russian medals in the 2008 Summer Olympics per medal class
SELECT
Country, 'Total', COUNT(*) AS Awards
FROM Summer_Medals
WHERE
Year = 2008 AND Country IN ('CHN', 'RUS')
GROUP BY Country, 2
ORDER BY Country ASC;
ROLLUP is a GROUP BY subclause that includes extra rows for group-level aggregations
GROUP BY Country, ROLLUP(Medal) will count all Country - and Medal -level totals, then
count only Country -level totals and fill in Medal with null s for these rows
ROLLUP is hierarchical, de-aggregating from the leftmost provided column to the right-most
ROLLUP(Country, Medal) includes Country -level totals
Group-level totals contain nulls ; the row with all null s is the grand total
Notice that it didn't include Medal -level totals, since it's ROLLUP(Country, Medal) and not
ROLLUP(Medal, Country)
CUBE(Year, Quarter)
Use ROLLUP when you have hierarchical
data (e.g., date parts) and don't want all Above rows + the following
possible group-level aggregations
| Year | Quarter | Sales |
Use CUBE when you want all possible | ------|---------|------- |
| null | Q1 | 33 |
group-level aggregations | null | Q2 | 42 |
POSTGRESQL SUMMARY STATS AND WINDOW FUNCTIONS
Let's practice!
POSTGRESQL SUMMARY STATS AND WINDOW FUNCTIONS
A survey of useful
functions
POSTGRESQL SUMMARY STATS AND WINDOW FUNCTIONS
Michel Semaan
Data Scientist
Nulls ahoy
Query Result
Pivoting
LAG and LEAD
| Letter |
|--------|
| A |
| B |
| C |
...into this
A,B,C
Country,
RANK() OVER (ORDER BY Medals DESC) AS Rank
FROM Country_Medals
ORDER BY Rank ASC;