diff --git a/02_activities/assignments/ABCbookstore_ERD.drawio.png b/02_activities/assignments/ABCbookstore_ERD.drawio.png deleted file mode 100644 index 9b4ff9ffc..000000000 Binary files a/02_activities/assignments/ABCbookstore_ERD.drawio.png and /dev/null differ diff --git a/02_activities/assignments/Assignment2.md b/02_activities/assignments/Assignment2.md index cea06a3e7..e2c6a0712 100644 --- a/02_activities/assignments/Assignment2.md +++ b/02_activities/assignments/Assignment2.md @@ -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: -- [y] Create a branch called `assignment-two`. -- [y] Ensure that the repository is public. -- [y] 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. -- [y] Verify that the link is accessible in a private browser window. +- [ ] 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. If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#cohort-6-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges. @@ -30,7 +30,7 @@ Steps to complete this part of the assignment: - Design a logical data model - Duplicate the logical data model and add another table to it following the instructions - Write, within this markdown file, an answer to Prompt 3 -git + ### Design a Logical Model @@ -55,40 +55,6 @@ The store wants to keep customer addresses. Propose two architectures for the CU ``` Your answer... -# keep customer addresses. Proposal 1 - retain changes (type 2) - -# Behavior: Each address change creates a new row. Historical addresses are preserved with effective / end dates or a current flag. -# Use to audit past addresses or track historical address. - - -CREATE TABLE customer_address_type2 ( - address_sk BIGINT PRIMARY KEY AUTOINCREMENT, - customer_id INT, - street VARCHAR(200), - city VARCHAR(100), - state VARCHAR(50), - postal_code VARCHAR(20), - country VARCHAR(50), - effective_from DATE NOT NULL, - effective_to DATE, - is_current BOOLEAN DEFAULT 1, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - -# keep customer addresses. Proposal 1 - overwrite changes (type 1) - -# Behavior: New address values replace the old values in-place. No history is kept. -# Use when your objective is to keep the most current address. - -CREATE TABLE customer_address_type1 ( - customer_id INT PRIMARY KEY, - street VARCHAR(200), - city VARCHAR(100), - state VARCHAR(50), - postal_code VARCHAR(20), - country VARCHAR(50), - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); ``` *** diff --git a/02_activities/assignments/assignment2.sql b/02_activities/assignments/assignment2.sql index 6f26aff90..0c50f41f0 100644 --- a/02_activities/assignments/assignment2.sql +++ b/02_activities/assignments/assignment2.sql @@ -20,9 +20,7 @@ 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 List" -FROM product; + --Windowed Functions /* 1. Write a query that selects from the customer_purchases table and numbers each customer’s @@ -34,38 +32,17 @@ 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, -ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) 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 -customer_id, -market_date -FROM ( - SELECT - customer_id, - market_date, - ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number - FROM customer_purchases -) AS recent_visits -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 -customer_id, -product_id, -market_date, -quantity, -COUNT(*) OVER (PARTITION BY customer_id, product_id) AS totalPurchases -FROM customer_purchases; -- String manipulations @@ -80,26 +57,11 @@ 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 INSTR(product_name, '-') > 0 - THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) - ELSE NULL - END AS description -FROM product; + /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ -SELECT -product_name, -CASE - WHEN INSTR(product_name, '-') > 0 - THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) - ELSE NULL - END AS description, product_size -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. @@ -111,36 +73,7 @@ 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. */ -WITH SalesByDate AS ( - SELECT - market_date, - SUM(quantity * cost_to_customer_per_qty) AS total_sales - FROM customer_purchases - GROUP BY market_date), - -RankedSales AS ( - SELECT - market_date, - total_sales, - RANK() OVER (ORDER BY total_sales DESC) AS rank_highest, - RANK() OVER (ORDER BY total_sales ASC) AS rank_lowest - FROM SalesByDate) - -SELECT -market_date, -total_sales, -'Best Day' AS description -FROM RankedSales -WHERE rank_highest = 1 - -UNION - -SELECT -market_date, -total_sales, -'Worst Day' AS description -FROM RankedSales -WHERE rank_lowest = 1; + /* SECTION 3 */ @@ -156,28 +89,7 @@ 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). */ -WITH VendorProducts AS ( - SELECT - v.vendor_name, - vi.product_id, - vi.original_price, -p.product_name - FROM vendor_inventory vi - JOIN vendor v ON v.vendor_id = vi.vendor_id -JOIN product p ON vi.product_id = p.product_id), - -AllCustomers AS ( - SELECT customer_id - FROM customer_purchases - GROUP BY customer_id) - -SELECT - vp.vendor_name, - vp.product_name, - COUNT(ac.customer_id) * 5 * vp.original_price AS total_sales_per_product -FROM VendorProducts vp -CROSS JOIN AllCustomers ac -GROUP BY vp.vendor_name, vp.product_name, vp.original_price; + -- INSERT /*1. Create a new table "product_units". @@ -185,54 +97,19 @@ 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 AS -SELECT - product_id, - product_name, - product_size, - product_category_id, - product_qty_type, - CURRENT_TIMESTAMP AS snapshot_timestamp -FROM product -WHERE 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 ( - 27, - 'Matcha latte', - '10 oz', - 3, - '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 rowid IN ( - SELECT pu.rowid - FROM product_units pu - JOIN ( - SELECT MIN(snapshot_timestamp) AS min_snapshot - FROM product_units - WHERE product_name = 'Matcha latte' - ) sub ON pu.snapshot_timestamp = sub.min_snapshot - WHERE pu.product_name = 'Matcha latte' -); + -- UPDATE /* 1.We want to add the current_quantity to the product_units table. @@ -251,29 +128,6 @@ 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 current_quantity INT; -WITH latest_quantities AS ( - SELECT - vi.product_id, - MAX(vi.market_date) AS latest_date - FROM vendor_inventory vi - GROUP BY vi.product_id -), -quantities_with_dates AS ( - SELECT - vi.product_id, - vi.quantity, - lq.latest_date - FROM vendor_inventory vi - INNER JOIN latest_quantities lq - ON vi.product_id = lq.product_id AND vi.market_date = lq.latest_date -) -UPDATE product_units -SET current_quantity = COALESCE( - (SELECT qwd.quantity - FROM quantities_with_dates qwd - WHERE qwd.product_id = product_units.product_id), 0); diff --git a/02_activities/assignments/bookstore_shifts.drawio.png b/02_activities/assignments/bookstore_shifts.drawio.png deleted file mode 100644 index 7e938beb7..000000000 Binary files a/02_activities/assignments/bookstore_shifts.drawio.png and /dev/null differ