Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ The store wants to keep customer addresses. Propose two architectures for the CU
**HINT:** search type 1 vs type 2 slowly changing dimensions.

```
Your answer...
Type 1 = addresses are overwritten
Type 2 = addresses are retained
```

***
Expand Down
Binary file not shown.
173 changes: 157 additions & 16 deletions 02_activities/assignments/Microcredential_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Edit the appropriate columns -- you're making two edits -- and the NULL rows wil
All the other rows will remain the same. */
--QUERY 1


SELECT
product_name || ', ' || coalesce(product_size,'') || ' (' || IFNULL(product_qty_type, "unit") || ')'
FROM product;


--END QUERY
Expand All @@ -42,6 +44,12 @@ Filter the visits to dates before April 29, 2022. */
--QUERY 2


SELECT DISTINCT
market_date,
customer_id,
dense_rank() OVER(PARTITION BY customer_id ORDER BY market_date ASC) as date_rank
FROM customer_purchases
WHERE date(market_date) < date('2022-04-29');


--END QUERY
Expand All @@ -54,6 +62,18 @@ HINT: Do not use the previous visit dates filter. */
--QUERY 3


SELECT x.*
,x.customer_id
FROM (
SELECT DISTINCT
customer_id
,market_date
,dense_rank() OVER(PARTITION BY customer_id ORDER BY market_date DESC) as date_rank
FROM customer_purchases
) x
INNER JOIN customer_purchases cp
on cp.customer_id = x.customer_id
WHERE x.date_rank = 1;


--END QUERY
Expand All @@ -66,8 +86,20 @@ You can make this a running count by including an ORDER BY within the PARTITION
Filter the visits to dates before April 29, 2022. */
--QUERY 4



SELECT x.*
, x.customer_id
FROM (
SELECT
customer_id
,market_date
,product_id
,count(*) OVER(PARTITION BY customer_id, product_id ORDER BY market_date ASC) as times_purchased_product
FROM customer_purchases

) x
INNER JOIN customer_purchases cp
on x.customer_id = cp.customer_id
WHERE date(x.market_date) < date('2022-04-29');

--END QUERY

Expand All @@ -85,7 +117,15 @@ Remove any trailing or leading whitespaces. Don't just use a case statement for
Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */
--QUERY 5


SELECT
product_id
,product_name
,CASE
WHEN instr(product_name,' - ') > 0 THEN substr(product_name,(instr(product_name,' - ')+3))
ELSE NULL
END product_description
FROM product
;


--END QUERY
Expand All @@ -94,7 +134,17 @@ Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR w
/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */
--QUERY 6


SELECT
product_id
,product_name
,CASE
WHEN instr(product_name,' - ') > 0 THEN substr(product_name,(instr(product_name,' - ')+3))
ELSE NULL
END product_description
,product_size
FROM product
WHERE product_size REGEXP '[0-9]'
;


--END QUERY
Expand All @@ -111,9 +161,40 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */
--QUERY 7




SELECT
market_date,
total_sales,
rn_max AS [row_number]

FROM (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales,
ROW_NUMBER() OVER (ORDER BY SUM(quantity * cost_to_customer_per_qty) DESC) AS rn_max

FROM customer_purchases
GROUP BY market_date
) x
WHERE rn_max = 1

UNION

SELECT
market_date,
total_sales,
rn_min

FROM (
SELECT
market_date
,SUM(quantity * cost_to_customer_per_qty) AS total_sales
,ROW_NUMBER() OVER (ORDER BY SUM(quantity * cost_to_customer_per_qty) ASC) AS rn_min
FROM customer_purchases
GROUP BY market_date
)
WHERE rn_min = 1;


--END QUERY


Expand All @@ -132,7 +213,39 @@ How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */
--QUERY 8

DROP TABLE IF EXISTS temp.vendor_products;
DROP TABLE IF EXISTS temp.customers;

-- how many distinct vendors and product names are there
CREATE TEMP TABLE IF NOT EXISTS temp.vendor_products AS
SELECT DISTINCT
vi.vendor_id
,vi.product_id
,vi.original_price
,p.product_name
,v.vendor_name
FROM vendor_inventory as vi
LEFT JOIN product AS p
ON vi.product_id = p.product_id
INNER JOIN vendor as v
ON vi.vendor_id = v.vendor_id;

-- how many unique customers
CREATE TEMP TABLE IF NOT EXISTS temp.customers AS
SELECT DISTINCT
customer_id
FROM customer;

SELECT * FROM temp.vendor_products;
SELECT * FROM temp.customers;

SELECT
vendor_name
,product_name
,original_price * 5 AS revenue_per_product
FROM temp.vendor_products JOIN temp.customers
GROUP BY vendor_name,product_name;



--END QUERY
Expand All @@ -145,8 +258,19 @@ It should use all of the columns from the product table, as well as a new column
Name the timestamp column `snapshot_timestamp`. */
--QUERY 9

DROP TABLE IF EXISTS product_units;


CREATE TABLE product_units AS
SELECT
product_id
,product_name
,product_size
,product_category_id
,product_qty_type
,datetime('now') AS CURRENT_TIMESTAMP
FROM product
WHERE product_qty_type = 'unit'
;

--END QUERY

Expand All @@ -155,8 +279,11 @@ Name the timestamp column `snapshot_timestamp`. */
This can be any product you desire (e.g. add another record for Apple Pie). */
--QUERY 10



INSERT INTO product_units
(product_id, product_name, product_size, product_category_id, product_qty_type, current_timestamp)
VALUES
(999, 'Garlic Bread', 'Large', 4, 'unit', datetime('now'))
;

--END QUERY

Expand All @@ -167,7 +294,9 @@ This can be any product you desire (e.g. add another record for Apple Pie). */
HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/
--QUERY 11


DELETE
FROM product_units
WHERE product_id = 999


--END QUERY
Expand All @@ -191,10 +320,22 @@ Finally, make sure you have a WHERE statement to update the right row,
When you have all of these components, you can run the update statement. */
--QUERY 12

ALTER TABLE product_units
ADD current_quantity INT;


UPDATE product_units
SET current_quantity = coalesce (
(
SELECT quantity
FROM (
SELECT
product_id
,quantity
,row_number() OVER(PARTITION BY product_id ORDER BY market_date DESC) AS rn_max
FROM vendor_inventory
)x
WHERE x.product_id = product_units.product_id AND rn_max = 1
)
,0);

--END QUERY