Created
November 24, 2021 17:44
-
-
Save Mr--John-Doe/b58155b86087c74000eed467bf59969c to your computer and use it in GitHub Desktop.
The Relational Thinking - Technique #1 - Inner Join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FABRICATE SOME EXAMPLES */ | |
WITH fake_sales AS ( | |
select '2020-12-01'::date dt, 100.00 revenue union all | |
select '2020-12-02'::date dt, 200.00 revenue union all | |
select '2020-12-08'::date dt, 300.00 revenue union all | |
select '2020-12-09'::date dt, 400.00 revenue union all | |
select '2020-12-10'::date dt, 500.00 revenue | |
) | |
, fake_dates AS ( | |
SELECT '2020-12-01'::date + SEQ4() dt | |
FROM TABLE (GENERATOR(ROWCOUNT => 31)) v | |
) | |
, | |
/* THE ACTUAL CODE */ | |
monthly_mtd_window AS ( | |
SELECT dt pivot_date, date_trunc(MONTH, dt) dt_from, dt dt_to | |
FROM fake_dates | |
WHERE dt < '2020-12-13'::date | |
) | |
SELECT pivot_date as date, sum(sales.revenue) as revenue_mtd | |
FROM fake_sales | |
INNER JOIN monthly_mtd_window | |
ON sales.dt BETWEEN dt_from and dt_to | |
group by pivot_date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment