diff --git a/02_activities/assignments/Microcredential_Cohort/Assignment2.md b/02_activities/assignments/Microcredential_Cohort/Assignment2.md index 2c39be5ff..d0628995c 100644 --- a/02_activities/assignments/Microcredential_Cohort/Assignment2.md +++ b/02_activities/assignments/Microcredential_Cohort/Assignment2.md @@ -57,6 +57,9 @@ The store wants to keep customer addresses. Propose two architectures for the CU ``` Your answer... + +One architecture would contain coulmns as customer_id, address, city, province, postal_code and country. if customer moves, we can simply update the address but will lost any previopus record (TYPE 1) +Second architecture would be to include above coulomns witha ddition 3 columns, start_date, end_date as well as current_status. when the address changes, a new address can be added and the end date will be the start date of the new address and if current_status is 1 it will mean that address is valid, if 0 it means that is the old record. (Type 2) ``` *** diff --git a/02_activities/assignments/Microcredential_Cohort/LMD_book_store_A2.png b/02_activities/assignments/Microcredential_Cohort/LMD_book_store_A2.png new file mode 100644 index 000000000..84b19f015 Binary files /dev/null and b/02_activities/assignments/Microcredential_Cohort/LMD_book_store_A2.png differ diff --git a/02_activities/assignments/Microcredential_Cohort/assignment2.sql b/02_activities/assignments/Microcredential_Cohort/assignment2.sql index 4079c18ae..4ddcb012e 100644 --- a/02_activities/assignments/Microcredential_Cohort/assignment2.sql +++ b/02_activities/assignments/Microcredential_Cohort/assignment2.sql @@ -22,13 +22,12 @@ 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. */ --QUERY 1 - - - +Select +COALESCE(product_name, '') || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' +From product --END QUERY - --Windowed Functions /* 1. Write a query that selects from the customer_purchases table and numbers each customer’s visits to the farmer’s market (labeling each market date with a different number). @@ -41,8 +40,13 @@ 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 ASC +) as 'customer_visit_number' +FROM customer_purchases +WHERE market_date < '2022-04-29' --END QUERY @@ -53,8 +57,16 @@ only the customer’s most recent visit. HINT: Do not use the previous visit dates filter. */ --QUERY 3 - - +SELECT * +FROM( + Select customer_id, market_date, + ROW_NUMBER() OVER + (PARTITION BY customer_id + ORDER BY market_date DESC + ) AS "customer_visit_number" + From customer_purchases +) AS visits +Where "customer_visit_number" = 1 --END QUERY @@ -66,8 +78,13 @@ 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' --END QUERY @@ -85,17 +102,22 @@ 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, +CASE + WHEN INSTR(product_name,'-') > 0 + THEN TRIM(SUBSTR(product_name, INSTR(product_name,'-') + 1)) + ELSE NULL +END AS description +From product --END QUERY /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ --QUERY 6 - - - +Select product_name, product_size +From product +Where product_size REGEXP '[0-9]' --END QUERY @@ -111,9 +133,42 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling with a UNION binding them. */ --QUERY 7 - - - +WITH daily_sales AS ( + SELECT + market_date, + SUM(quantity * cost_per_quantity) AS total_sales + FROM customer_purchases + GROUP BY market_date +), + +ranked_sales AS ( + SELECT + market_date, + total_sales, + ROW_NUMBER() OVER ( + ORDER BY total_sales DESC + ) AS best_day, + ROW_NUMBER() OVER ( + ORDER BY total_sales ASC + ) AS worst_day + FROM daily_sales +) + +SELECT + market_date, + total_sales, + 'best day' AS day_type +FROM ranked_sales +WHERE best_day = 1 + +UNION + +SELECT + market_date, + total_sales, + 'worst day' AS day_type +FROM ranked_sales +WHERE worst_day = 1; --END QUERY @@ -132,9 +187,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, +SUM(vi.original_price * 5) AS total_sales +FROM vendor_inventory vi +JOIN vendor AS v +ON vi.vendor_id = v.vendor_id +JOIN product AS p +ON vi.product_id = p.product_id +CROSS JOIN ( + SELECT COUNT(DISTINCT customer_id) AS customer_count FROM customer) AS c +GROUP BY v.vendor_name, p.product_name --END QUERY @@ -144,9 +206,11 @@ 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 @@ -154,10 +218,8 @@ Name the timestamp column `snapshot_timestamp`. */ /*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_category_id, product_qty_type, snapshot_timestamp) +VALUES (257, 'Apple Pie', '1 pie', '10', 'unit', CURRENT_TIMESTAMP); --END QUERY @@ -167,12 +229,11 @@ 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 = 7; --END QUERY - -- UPDATE /* 1.We want to add the current_quantity to the product_units table. First, add a new column, current_quantity to the table using the following syntax. @@ -190,9 +251,29 @@ 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. */ --QUERY 12 +ALTER TABLE product_units +ADD current_quantity INT; - - +UPDATE product_units +SET current_quantity = COALESCE( + ( + SELECT quantity + FROM ( + SELECT + product_id, + quantity, + market_date, + ROW_NUMBER() OVER ( + PARTITION BY product_id + ORDER BY market_date DESC + ) AS rn + FROM vendor_inventory + ) AS latest + WHERE latest.product_id = product_units.product_id + AND latest.rn = 1 + ), + 0 +); --END QUERY