Building complex calculations

Reportistica in SQL

Tyler Pernes

Learning & Development Consultant

Approaches

  1. Window functions
  2. Layered calculations
Reportistica in SQL

Window functions

  • References other rows in the table.
Reportistica in SQL

Window functions

  • References other rows in the table.

Reportistica in SQL

Window functions

  • References other rows in the table.

Reportistica in SQL

Window function syntax

SUM(value) OVER (PARTITION BY field ORDER BY field)

Reportistica in SQL

Window function syntax

SUM(value) OVER (PARTITION BY field ORDER BY field)

Reportistica in SQL

Window function syntax

SUM(value) OVER (PARTITION BY field ORDER BY field)

  • PARTITION BY = range of calculation
  • ORDER BY = order of rows when running calculation
Reportistica in SQL

Window function examples

Total bronze medals

SELECT 
    country_id, 
    athlete_id, 
    SUM(bronze) OVER () AS total_bronze
FROM summer_games;
+-------------+-------------+---------------+
| country_id  | athlete_id  | total_bronze  |
|-------------|-------------|---------------|
| 11          | 77505       | 141           |
| 11          | 11673       | 141           |
| 14          | 85554       | 141           |
| 14          | 76433       | 141           |
+-------------+-------------+---------------+
Reportistica in SQL

Window function examples

Country bronze medals

SELECT 
    country_id, 
    athlete_id, 
    SUM(bronze) OVER (PARTITION BY country_id) AS total_bronze
FROM summer_games
+-------------+-------------+---------------+
| country_id  | athlete_id  | total_bronze  |
|-------------|-------------|---------------|
| 11          | 77505       | 12            |
| 11          | 11673       | 12            |
| 14          | 85554       | 5             |
| 14          | 76433       | 5             |
+-------------+-------------+---------------+
Reportistica in SQL

Types of window functions

  • SUM()
  • AVG()
  • MIN()
  • MAX()
Reportistica in SQL

Types of window functions

  • LAG() and LEAD()

Reportistica in SQL

Types of window functions

  • LAG() and LEAD()

Reportistica in SQL

Types of window functions

  • ROW_NUMBER() and RANK()

Reportistica in SQL

Window function on an aggregation

original_table
+----------+-----------+---------+ 
| team_id  | player_id | points  |
|----------|-----------|---------|    
| 1        | 4123      | 3       |
| 1        | 5231      | 6       |
| 2        | 8271      | 5       |
+----------+-----------+---------+
desired_report
+----------+-------------+---------------+ 
| team_id  | team_points | league_points |
|----------|-------------|---------------|    
| 1        | 9           | 43            |
| 2        | 12          | 43            |
| 3        | 22          | 43            |
+----------+-------------+---------------+
Reportistica in SQL

Window function on an aggregation

Final query

SELECT
    team_id,
    SUM(points) AS team_points,
    SUM(SUM(points)) OVER () AS league_points
FROM original_table
GROUP BY team_id;
Reportistica in SQL

Window function on an aggregation

SELECT
    team_id,
    SUM(points) AS team_points,
    SUM(points) OVER () AS league_points
FROM original_table
GROUP BY team_id;
ERROR: points must be an aggregation or appear in a GROUP BY statement.
Reportistica in SQL

Layered calculations

  • Aggregate an existing aggregation
  • Leverages a subquery
Reportistica in SQL

Layered calculations example

Step 1: Total bronze medals per country

SELECT country_id, SUM(bronze) as bronze_medals
FROM summer_games
GROUP BY country_id;

Step 2: Convert to subquery and take the max

SELECT MAX(bronze_medals)
FROM
  (SELECT country_id, SUM(bronze) as bronze_medals
  FROM summer_games
  GROUP BY country_id) AS subquery;
Reportistica in SQL

Planning out complex calculations

Reportistica in SQL

Planning out complex calculations

  • Ordering for window function?
  • Two aggregations with a layered calculation?
Reportistica in SQL

Let's practice!

Reportistica in SQL

Preparing Video For Download...