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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion 02_activities/assignments/Cohort_8/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ 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...
If the store wants to retain changes for the CUSTOMER_ADDRESS table, the type 2 slowly changing dimension architecture is recommended because it creates a new customer address record for each change to the historical data it has for the customer address. When a customer address changes, a new row with the updated address will be created in the table. While the old address for the customer is still retained in the table. This table would need a way to identify the current address, such as by having an is_active column that indicates the current address, or a timestamp column that could tell us the most recent address. This table can have many rows per customer.

Whereas, if the store wants to overwrite the previous customer address in the table when there is a new customer address, the type 1 slowly changing dimensions is recommended. The new customer address will replace the old address in the row. No historical data will be retained. This table will have exactly one row per customer.

In either design, the customer_address table would have columns such as street name, house number, postal code, city, province/state, and country.

```

***
Expand Down
188 changes: 183 additions & 5 deletions 02_activities/assignments/Cohort_8/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ The `||` values concatenate the columns into strings.
Edit the appropriate columns -- you're making two edits -- and the NULL rows will be fixed.
All the other rows will remain the same.) */


SELECT
product_name || ', ' || coalesce(product_size, '')|| ' (' || coalesce(product_qty_type, 'unit') || ')' as list_of_products
FROM product;

--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -32,17 +34,52 @@ each new market date for each customer, or select only the unique market dates p
(without purchase details) and number those visits.
HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */

SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date ASC) as number_of_customer_visits
FROM customer_purchases;


/* 2. Reverse the numbering of the query from a part so each customer’s most recent visit is labeled 1,
then write another query that uses this one as a subquery (or temp table) and filters the results to
only the customer’s most recent visit. */

DROP TABLE IF EXISTS TEMP.customer_most_recent_visit;

CREATE TEMP TABLE IF NOT EXISTS TEMP.customer_most_recent_visit
(
customer_id INT,
market_date date,
number_of_customer_visits INT
);

INSERT INTO TEMP.customer_most_recent_visit
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC) as number_of_customer_visits
FROM customer_purchases;

SELECT DISTINCT
customer_id,
market_date
FROM TEMP.customer_most_recent_visit
WHERE number_of_customer_visits = 1;

/* 3. Using a COUNT() window function, include a value along with each row of the
customer_purchases table that indicates how many different times that customer has purchased that product_id. */

SELECT
product_id,
customer_id,
COUNT(*) as customer_purchases_count
FROM customer_purchases
GROUP BY product_id, customer_id;


-- String manipulations
Expand All @@ -57,11 +94,25 @@ 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. */


SELECT
product_name,
CASE
WHEN product_name like '%-%' THEN Trim(SUBSTR (product_name, INSTR(product_name,'-') +1))
ELSE NULL
END as product_description
FROM product;

/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */


SELECT
product_name,
product_size,
CASE
WHEN product_name like '%-%' THEN Trim(SUBSTR (product_name, INSTR(product_name,'-') +1))
ELSE NULL
END as product_description
FROM product
WHERE product_size REGEXP '[0-9]';

-- UNION
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
Expand All @@ -73,7 +124,52 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
3) Query the second temp table twice, once for the best day, once for the worst day,
with a UNION binding them. */

DROP TABLE IF EXISTS TEMP.market_dates_total_sales;

CREATE TEMP TABLE IF NOT EXISTS TEMP.market_dates_total_sales
(
market_date date,
total_sales decimal(16,2)
);

INSERT INTO TEMP.market_dates_total_sales
SELECT
market_date,
sum(cost_to_customer_per_qty*quantity)
FROM customer_purchases
GROUP BY market_date;

DROP TABLE IF EXISTS TEMP.market_dates_total_sales_rank;

CREATE TEMP TABLE IF NOT EXISTS TEMP.market_dates_total_sales_rank
(
market_date date,
total_sales_best_day decimal(16,2),
total_sales_worst_day decimal (16,2)
);

INSERT INTO TEMP.market_dates_total_sales_rank
SELECT
market_date,
row_number() OVER (
ORDER BY total_sales DESC) as total_sales_best_day,
row_number() OVER (
ORDER BY total_sales ASC) as total_sales_worst_day
FROM market_dates_total_sales;

SELECT
'best day' as best_or_worst_day,
market_date
FROM market_dates_total_sales_rank
WHERE total_sales_best_day = 1

UNION

SELECT
'worst day'as best_or_worst_day,
market_date
FROM market_dates_total_sales_rank
WHERE total_sales_worst_day = 1;


/* SECTION 3 */
Expand All @@ -89,27 +185,76 @@ Think a bit about the row counts: how many distinct vendors, product names are t
How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */


SELECT
vendor_name,
product_name,
sum(original_price * 5)
FROM vendor
INNER JOIN vendor_inventory on vendor.vendor_id = vendor_inventory.vendor_id
INNER JOIN (
SELECT product_name, product_id
FROM product
CROSS JOIN customer
) p on vendor_inventory.product_id = p.product_id
GROUP BY vendor_name, product_name;

-- INSERT
/*1. Create a new table "product_units".
This table will contain only products where the `product_qty_type = 'unit'`.
It should use all of the columns from the product table, as well as a new column for the `CURRENT_TIMESTAMP`.
Name the timestamp column `snapshot_timestamp`. */

DROP TABLE IF EXISTS product_units;

CREATE TABLE product_units(
product_id INT,
product_name varchar(45),
product_size varchar(45),
product_category_id INT,
product_qty_type varchar(45),
snapshot_timestamp datetime);
INSERT INTO product_units
SELECT
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
CURRENT_TIMESTAMP
FROM product
WHERE product_qty_type = 'unit';

SELECT *
FROM product_units;

/*2. Using `INSERT`, add a new row to the product_units table (with an updated timestamp).
This can be any product you desire (e.g. add another record for Apple Pie). */

INSERT INTO product_units
SELECT
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
CURRENT_TIMESTAMP
FROM product
WHERE product_name = 'Cherry Pie';

SELECT *
FROM product_units;

-- DELETE
/* 1. Delete the older record for the whatever product you added.

HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/

DELETE FROM product_units
Where product_name = 'Cherry Pie'
AND snapshot_timestamp = (SELECT min (snapshot_timestamp) FROM product_units WHERE product_name = 'Cherry Pie');

SELECT *
FROM product_units;

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -129,5 +274,38 @@ 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. */


ALTER TABLE product_units
ADD current_quantity INT;


DROP TABLE IF EXISTS TEMP.last_quantity_per_product;

CREATE TEMP TABLE IF NOT EXISTS TEMP.last_quantity_per_product
(
product_id INT,
vendor_id INT,
max_market_date date
);

INSERT INTO TEMP.last_quantity_per_product
SELECT
product_id,
vendor_id,
max(market_date)
FROM vendor_inventory
GROUP BY product_id, vendor_id;

UPDATE product_units
SET current_quantity = (
SELECT coalesce(vendor_inventory.quantity, 0)
FROM product
LEFT JOIN last_quantity_per_product
ON product.product_id = last_quantity_per_product.product_id
LEFT JOIN vendor_inventory
ON last_quantity_per_product.product_id = vendor_inventory.product_id
AND last_quantity_per_product.vendor_id = vendor_inventory.vendor_id
AND last_quantity_per_product.max_market_date = vendor_inventory.market_date
WHERE product_units.product_id = product.product_id
);

SELECT *
FROM product_units;