Created
October 28, 2020 16:47
-
-
Save michaelminter/7fd5a28f4bb72a72619e7473cc4f3a8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
CONTEXT: | |
- add a brief description of why we need this query | |
RESULT EXPECTATION | |
- add a brief description of your expectations for the query result | |
ASSUMPTION: | |
- add assumption about business logic | |
- add assumption about data | |
*/ | |
/* | |
CONTEXT: | |
- Our company wants to understand if COVID has any impact on sales in stores | |
around Chicago. | |
RESULT EXPECTATION | |
- This query returns total sales (in USD) for each of our stores in Chicago | |
every month before and after COVID, starting from 2019-03-01. | |
ASSUMPTION: | |
- Dates before 2020-03-01 are considered "Before COVID" | |
- Each transaction has a unique id, so we do not expect duplications | |
in our transaction table | |
- There are some spam transactions we have identified after COVID, | |
so we will filter these out | |
*/ | |
SELECT | |
store_info.id, | |
store_info.name AS store_name, | |
DATE_FORMAT(transactions.date, "%Y-%m") AS transaction_month, | |
SUM(transactions.total_amount) AS total_amount | |
FROM | |
transactions | |
LEFT JOIN | |
-- get all stores in Chicago | |
( | |
SELECT | |
id, | |
name | |
FROM | |
stores | |
WHERE | |
city = 'Chicago' | |
) AS store_info | |
ON | |
transactions.branch_id = store_info.id | |
WHERE | |
transactions.date >= '2019-03-01' | |
-- filter spam transactions | |
AND transactions.id NOT IN | |
( | |
SELECT | |
id | |
FROM | |
spam_transactions | |
) | |
GROUP BY | |
store_info.id, | |
store_info.name, | |
DATE_FORMAT(transactions.date, "%Y-%m") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment