diff --git a/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic ( Shift +).png b/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic ( Shift +).png new file mode 100644 index 000000000..701e781fb Binary files /dev/null and b/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic ( Shift +).png differ diff --git a/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic.png b/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic.png new file mode 100644 index 000000000..5292034c3 Binary files /dev/null and b/02_activities/assignments/Microcredential_Cohort/Assignment 2 Logic.png differ diff --git a/02_activities/assignments/Microcredential_Cohort/Assignment2.md b/02_activities/assignments/Microcredential_Cohort/Assignment2.md index 2c39be5ff..028daee2d 100644 --- a/02_activities/assignments/Microcredential_Cohort/Assignment2.md +++ b/02_activities/assignments/Microcredential_Cohort/Assignment2.md @@ -37,18 +37,11 @@ Steps to complete this part of the assignment: #### Prompt 1 Design a logical model for a small bookstore. 📚 -At the minimum it should have employee, order, sales, customer, and book entities (tables). Determine sensible column and table design based on what you know about these concepts. Keep it simple, but work out sensible relationships to keep tables reasonably sized. - -Additionally, include a date table. -A date table (also called a calendar table) is a permanent table containing a list of dates and various components of those dates. Some theory, tips, and commentary can be found [here](https://www.sqlshack.com/designing-a-calendar-table/), [here](https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/) and [here](https://sqlgeekspro.com/creating-calendar-table-sql-server/). -Remember, you don't actually need to run any of the queries in these articles, but instead understand *why* date tables in SQL make sense, and how to situate them within your logical models. - -There are several tools online you can use, I'd recommend [Draw.io](https://www.drawio.com/) or [LucidChart](https://www.lucidchart.com/pages/). - -**HINT:** You do not need to create any data for this prompt. This is a conceptual model only. +File location - /C:\Users\param\OneDrive\Desktop\DSI\Repo\sql\02_activities\assignments\Microcredential_Cohort\Assignment 2 Logic.png #### Prompt 2 -We want to create employee shifts, splitting up the day into morning and evening. Add this to the ERD. + +File Location - /C:\Users\param\OneDrive\Desktop\DSI\Repo\sql\02_activities\assignments\Microcredential_Cohort\Assignment 2 Logic ( Shift +).png #### Prompt 3 The store wants to keep customer addresses. Propose two architectures for the CUSTOMER_ADDRESS table, one that will retain changes, and another that will overwrite. Which is type 1, which is type 2? @@ -57,7 +50,33 @@ The store wants to keep customer addresses. Propose two architectures for the CU ``` Your answer... -``` + +Architecture 1: TYPE 1 + +Table Structure + +CUSTOMER ADDRESS (Type 1) + + - address id + - customer id + - street address + - city + - state + - zip code + - country + + CUSTOMER ADDRESS (Type 2) + + - address id + - customer id + - street address + - city + - state + - zip code + - country + - effective date (when address became current) + - end date (when customer moved - NULL if current) + - is current (boolean: TRUE if current, FALSE if historical) *** diff --git a/02_activities/assignments/Microcredential_Cohort/assignment2.sql b/02_activities/assignments/Microcredential_Cohort/assignment2.sql index 4079c18ae..90db40fda 100644 --- a/02_activities/assignments/Microcredential_Cohort/assignment2.sql +++ b/02_activities/assignments/Microcredential_Cohort/assignment2.sql @@ -23,7 +23,8 @@ 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, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' +from product; --END QUERY @@ -41,8 +42,11 @@ HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). Filter the visits to dates before April 29, 2022. */ --QUERY 2 - - +SELECT customer_id, market_date, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) as visit_number +FROM customer_purchases +WHERE market_date < '2022-04-29' +ORDER BY customer_id, market_date; --END QUERY @@ -53,8 +57,10 @@ only the customer’s most recent visit. HINT: Do not use the previous visit dates filter. */ --QUERY 3 - - +SELECT customer_id, market_date, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) as visit_number +FROM customer_purchases +ORDER BY customer_id, market_date; --END QUERY @@ -66,8 +72,11 @@ 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 customer_id, product_id, market_date, +COUNT(product_id) OVER (PARTITION BY customer_id, product_id ORDER BY market_date) as purchase_count +FROM customer_purchases +WHERE market_date < '2022-04-29' +ORDER BY customer_id, product_id, market_date; --END QUERY @@ -85,7 +94,10 @@ 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_name, +LTRIM(RTRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))) AS description,INSTR(product_name, '-') AS hyphen_position +FROM product +WHERE INSTR(product_name, '-') > 0; --END QUERY @@ -93,9 +105,10 @@ 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, product_size +FROM product +WHERE product_size REGEXP '[0-9]' +ORDER BY product_id; --END QUERY @@ -111,7 +124,38 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling with a UNION binding them. */ --QUERY 7 +-- CTE 1: Calculate total sales by date +WITH daily_sales AS ( + SELECT + market_date, + SUM(quantity * cost_per_quantity) AS total_sales + FROM customer_purchases + GROUP BY market_date +), + +-- CTE 2: Rank the dates (best and worst) +ranked_days AS ( + SELECT + market_date, + total_sales, + RANK() OVER (ORDER BY total_sales DESC) AS best_rank, + RANK() OVER (ORDER BY total_sales ASC) AS worst_rank + FROM daily_sales +) + +-- Query for best day +SELECT market_date, total_sales, 'Best Day' AS day_type +FROM ranked_days +WHERE best_rank = 1 +UNION + +-- Query for worst day +SELECT market_date, total_sales, 'Worst Day' AS day_type +FROM ranked_days +WHERE worst_rank = 1 + +ORDER BY total_sales DESC; --END QUERY @@ -132,8 +176,16 @@ How many customers are there (y). Before your final group by you should have the product of those two queries (x*y). */ --QUERY 8 - - +SELECT + v.vendor_name, + p.product_name, + 5 * COUNT(DISTINCT c.customer_id) * AVG(c.cost_per_quantity) AS total_revenue +FROM vendor_inventory vi +JOIN vendor v ON vi.vendor_id = v.vendor_id +JOIN product p ON vi.product_id = p.product_id +JOIN customer_purchases c ON vi.vendor_id = c.vendor_id AND vi.product_id = c.product_id +GROUP BY v.vendor_name, p.product_name +ORDER BY v.vendor_name, p.product_name; --END QUERY @@ -144,19 +196,21 @@ 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`. */ --QUERY 9 - - +CREATE TABLE product_units AS +SELECT *, CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_qty_type = 'unit'; --END QUERY + /*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). */ --QUERY 10 - - - +insert into product_units (product_id, product_name, product_size, product_qty_type, snapshot_timestamp) +values (100, 'Apple Pie', '1 slice', 'unit', CURRENT_TIMESTAMP); --END QUERY @@ -167,7 +221,8 @@ 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_name = 'Apple Pie' and snapshot_timestamp < (select max(snapshot_timestamp) from product_units where product_name = 'Apple Pie'); --END QUERY @@ -191,7 +246,14 @@ 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 - +UPDATE product_units +SET current_quantity = COALESCE( + (SELECT MAX(quantity) + FROM vendor_inventory + WHERE vendor_inventory.product_id = product_units.product_id), + 0 +) +WHERE product_units.product_id IS NOT NULL; --END QUERY