Comparing dates

Reporting in SQL

Tyler Pernes

Learning & Development Consultant

Questions to answer

  1. Last month vs previous month?
  2. Rolling 7 days?
original_table
+------------+----------+
| date       | revenue  |
|------------+----------+
| 2018-01-01 | 400      |
| 2018-01-02 | 380      |
| 2018-01-03 | 625      |
+------------+----------+
Reporting in SQL

Month-over-month comparison

  • LAG(value, offset) outputs a value from an offset number of rows previous to the current row.
  • LEAD(value, offset) outputs a value from an offset number of rows after the current row.

Reporting in SQL

Month-over-month comparison

Step 1: show revenue by month

SELECT
    DATE_PART('month',date) AS month,
    SUM(revenue) as current_rev
FROM original_table
GROUP BY month;

Step 2: previous month's revenue

SELECT
    DATE_PART('month',date) AS month,
    SUM(revenue) as current_rev,
    LAG(SUM(revenue)) OVER (ORDER BY DATE_PART('month',date)) AS prev_rev
FROM original_table
GROUP BY month;
Reporting in SQL

Month-over-month comparison

Step 3: percent change calculation

SELECT
    DATE_PART('month',date) AS month,
    SUM(revenue) as current_rev,
    LAG(SUM(revenue)) OVER (ORDER BY DATE_PART('month',date)) AS prev_rev,
    SUM(revenue)
    /
    LAG(SUM(revenue)) OVER (ORDER BY DATE_PART('month',date))-1 AS perc_change
FROM original_table
GROUP BY month;
Reporting in SQL

Month-over-month comparison

Step 3: percent change calculation

+--------+--------------+-----------+--------------+
| month  | current_rev  | prev_rev  | perc_change  |
|--------+--------------+-----------+--------------|
| 01     | 15000        | null      | null         |
| 02     | 14000        | 15000     | -.06         |
| 03     | 21000        | 14000     | .50          |
+--------+--------------+-----------+--------------+
Reporting in SQL

Rolling calculations

  • Only take into account 7 rows

New clause: ROWS BETWEEN

SUM(value) OVER (ORDER BY value ROWS BETWEEN N PRECEDING AND CURRENT ROW)
Reporting in SQL

Rolling calculations

Rolling sum query

SELECT 
    date,
    SUM(SUM(revenue)) OVER 
    (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS weekly_revenue
FROM original_table
GROUP BY date;
Reporting in SQL

New table: web_data

web_data
+------------+-------------+--------+
| date       | country_id  | view   |
|------------+-------------+--------|
| 2018-01-01 | 1           | 24313  |
| 2018-01-01 | 2           | 3768   |
| 2018-01-01 | 3           | 26817  |
+------------+-------------+--------+
Reporting in SQL

Let's practice!

Reporting in SQL

Preparing Video For Download...