Reporting in SQL
Tyler Pernes
Learning & Development Consultant
original_table
+------------+----------+
| date | revenue |
|------------+----------+
| 2018-01-01 | 400 |
| 2018-01-02 | 380 |
| 2018-01-03 | 625 |
+------------+----------+
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.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;
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;
Step 3: percent change calculation
+--------+--------------+-----------+--------------+
| month | current_rev | prev_rev | perc_change |
|--------+--------------+-----------+--------------|
| 01 | 15000 | null | null |
| 02 | 14000 | 15000 | -.06 |
| 03 | 21000 | 14000 | .50 |
+--------+--------------+-----------+--------------+
New clause: ROWS BETWEEN
SUM(value) OVER (ORDER BY value ROWS BETWEEN N PRECEDING AND CURRENT ROW)
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;
web_data
+------------+-------------+--------+
| date | country_id | view |
|------------+-------------+--------|
| 2018-01-01 | 1 | 24313 |
| 2018-01-01 | 2 | 3768 |
| 2018-01-01 | 3 | 26817 |
+------------+-------------+--------+
Reporting in SQL