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
13 changes: 8 additions & 5 deletions 02_activities/assignments/Cohort_8/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.

Checklist:
- [ ] Create a branch called `assignment-two`.
- [ ] Ensure that the repository is public.
- [ ] Review [the PR description guidelines](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md#guidelines-for-pull-request-descriptions) and adhere to them.
- [ ] Verify that the link is accessible in a private browser window.
- [X] Create a branch called `assignment-two`.
- [X] Ensure that the repository is public.
- [X] Review [the PR description guidelines](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md#guidelines-for-pull-request-descriptions) and adhere to them.
- [X] Verify that the link is accessible in a private browser window.

If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges.

Expand Down Expand Up @@ -54,7 +54,10 @@ 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 slowly changing dimension (SCD) does not preserve history, and the purpose is only to retain the most current information, so Type 1 will overwrite (update) changes. Type 2 SCD preserves and tracks history and enables time-based analysis.
To store customer addresses in a CUSTOMER_ADDRESS table, the address fields in the current CUSTOMER table can be removed. A separate CUSTOMER_ADDRESS table can be created where ADDR_ID is the PK, and CUSTOMER_ID is the FK to the CUSTOMER table.
In the CUSTOMER_ADDRESS table, to implement SCD Type 2, start_date and end_date columns can be added (in addition to address, postal code etc.) that track where a customer resided previously in the event that a customer changed an address; if a customer changes an address, a new row gets created, the end_date column for the previous entry gets updated, and the start_date column of the current entry gets updated, with the end_date for the current entry being NULL.
To implement SCD Type 1, where the address is to be updated, you don't need the start_date and end_date columns, you can just have the usual address, postal code etc. columns to store the address details, and these columns would get updated when an address changes.
```

***
Expand Down
Binary file not shown.
Binary file not shown.
135 changes: 121 additions & 14 deletions 02_activities/assignments/Cohort_8/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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 [Detailed_Product_Request]
FROM product;


--Windowed Functions
Expand All @@ -32,18 +35,41 @@ 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 visit_number
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. */

SELECT *
FROM (
SELECT
customer_id,
market_date,
dense_rank() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM customer_purchases
)
WHERE visit_number = 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 *,
count(*) OVER (
PARTITION BY customer_id, product_id
) AS cust_prod_times_purchased
FROM customer_purchases;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -56,11 +82,28 @@ Remove any trailing or leading whitespaces. Don't just use a case statement for
| Habanero Peppers - Organic | Organic |

Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */

SELECT *,
CASE
WHEN INSTR(product_name,'-') > 0
THEN TRIM(
SUBSTR(product_name, INSTR(product_name, '-')+2)
)
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 *,
CASE
WHEN INSTR(product_name,'-') > 0
THEN TRIM(
SUBSTR(product_name, INSTR(product_name, '-')+2)
)
ELSE NULL
END AS product_description
FROM product
WHERE product_size REGEXP '[0-9]';


-- UNION
Expand All @@ -73,9 +116,39 @@ 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. */




--Step 1: Aggregate sales by market_date
WITH TotalSalesByDate AS (
SELECT
market_date,
SUM(cp.quantity * cp.cost_to_customer_per_qty) as total_sales
FROM customer_purchases cp
GROUP BY market_date
),
--Step 2: Add best/worst rank based on return of previous query
RankedSalesByDate AS (
SELECT
market_date,
total_sales,
dense_rank() OVER(ORDER BY total_sales DESC) as best_day,
dense_rank() OVER(ORDER BY total_sales ASC) as worst_day
FROM TotalSalesByDate
GROUP BY market_date
)
--Step 3: Find best and worst days and create the UNION
SELECT
market_date,
total_sales,
'Best Day' as [Performance]
FROM RankedSalesByDate
WHERE best_day = 1
UNION
SELECT
market_date,
total_sales,
'Worst Day' as [Performance]
FROM RankedSalesByDate
WHERE worst_day = 1;

/* SECTION 3 */

-- Cross Join
Expand All @@ -88,27 +161,48 @@ Remember, CROSS JOIN will explode your table rows, so CROSS JOIN should likely b
Think a bit about the row counts: how many distinct vendors, product names are there (x)?
How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */

--There are 3 distinct vendors in the vendor_inventory table and 8 distinct products
WITH CustomerCount AS (
SELECT COUNT(*) AS total_customers
FROM customer
)
SELECT DISTINCT
v.vendor_name,
p.product_name,
(5 * vi.original_price * c.total_customers) as potential_revenue
FROM vendor_inventory vi
INNER JOIN vendor v
ON vi.vendor_id = v.vendor_id
INNER JOIN product p
ON vi.product_id = p.product_id
CROSS JOIN CustomerCount c;


-- 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`. */

CREATE TABLE product_units AS
SELECT
p.*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product p
WHERE p.product_qty_type = 'unit';


/*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 (product_id, product_name, product_size, product_category_id, product_qty_type, snapshot_timestamp)
VALUES (99, 'pumpkin pie', '8"', 89, 'unit', CURRENT_TIMESTAMP);


-- 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_id = 99;


-- UPDATE
Expand All @@ -128,6 +222,19 @@ Finally, make sure you have a WHERE statement to update the right row,
you'll need to use product_units.product_id to refer to the correct row within the product_units table.
When you have all of these components, you can run the update statement. */




ALTER TABLE product_units
ADD COLUMN current_quantity INTEGER;
UPDATE product_units
SET current_quantity = coalesce((
SELECT
vi.quantity
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
AND vi.market_date =
(SELECT
MAX(vi1.market_date)
FROM vendor_inventory vi1
WHERE product_units.product_id = vi1.product_id
GROUP BY product_id
)
),0);