Skip to content

Instantly share code, notes, and snippets.

@michaelminter
Created October 28, 2020 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelminter/7fd5a28f4bb72a72619e7473cc4f3a8c to your computer and use it in GitHub Desktop.
Save michaelminter/7fd5a28f4bb72a72619e7473cc4f3a8c to your computer and use it in GitHub Desktop.
/*
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