diff --git a/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type1.drawio.pdf b/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type1.drawio.pdf new file mode 100644 index 000000000..e2de3b033 Binary files /dev/null and b/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type1.drawio.pdf differ diff --git a/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type2.drawio.pdf b/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type2.drawio.pdf new file mode 100644 index 000000000..c468ce2c2 Binary files /dev/null and b/02_activities/assignments/Cohort_8/Assignment 2 Diagram_type2.drawio.pdf differ diff --git a/02_activities/assignments/Cohort_8/assignment2.sql b/02_activities/assignments/Cohort_8/assignment2.sql index c2743d3b7..52a0120b1 100644 --- a/02_activities/assignments/Cohort_8/assignment2.sql +++ b/02_activities/assignments/Cohort_8/assignment2.sql @@ -21,7 +21,14 @@ 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. */ +DROP VIEW IF EXISTS list_for_products; +CREATE VIEW IF NOT EXISTS list_for_products AS +SELECT +coalesce (product_name, '') || ', ' || coalesce (product_size, '') || ' (' || coalesce (product_qty_type, 'unit') || ')' AS list_of_products +FROM product; + +SELECT * from list_for_products ; --Windowed Functions @@ -34,18 +41,46 @@ 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, + transaction_time, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date, transaction_time) AS No_of_visit +FROM customer_purchases +ORDER BY customer_id, market_date, transaction_time; /* 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, + transaction_time +FROM ( + SELECT + customer_id, + market_date, + transaction_time, + DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS No_of_visit + FROM customer_purchases +) AS ranked_visits +WHERE No_of_visit= 1 +ORDER BY customer_id; /* 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, + transaction_time, + COUNT(*) OVER (PARTITION BY customer_id, product_id) AS times_purchased_this_product +FROM customer_purchases +ORDER BY customer_id, product_id, market_date; -- String manipulations /* 1. Some product names in the product table have descriptions like "Jar" or "Organic". @@ -59,10 +94,32 @@ 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 value_of_product +FROM product; /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ +SELECT + product_id, + product_name, + product_size, + CASE + WHEN INSTR(product_name, '-') > 0 THEN + TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) + ELSE + NULL + END AS value_of_product +FROM product +WHERE product_size REGEXP '[0-9]' +ORDER BY product_name; -- UNION @@ -75,6 +132,43 @@ 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: +WITH daily_sales AS ( + SELECT + market_date, + SUM(quantity * cost_to_customer_per_qty) AS total_sales + FROM customer_purchases + GROUP BY market_date +), + +-- Step 2: +ranked_sales AS ( + SELECT + market_date, + total_sales, + RANK() OVER (ORDER BY total_sales DESC) AS rank_best, + RANK() OVER (ORDER BY total_sales ASC) AS rank_worst + FROM daily_sales +) + +-- Step 3: +SELECT + market_date, + total_sales, + 'Highest Sales' AS sales_type +FROM ranked_sales +WHERE rank_best = 1 + +UNION + +SELECT + market_date, + total_sales, + 'Lowest Sales' AS sales_type +FROM ranked_sales +WHERE rank_worst = 1 + +ORDER BY total_sales DESC; @@ -92,6 +186,24 @@ How many customers are there (y). Before your final group by you should have the product of those two queries (x*y). */ +SELECT + v.vendor_name, + p.product_name, + COUNT(DISTINCT c.customer_id) AS number_of_customers, + COUNT(DISTINCT c.customer_id) * 5 AS total_units_sold, + COUNT(DISTINCT c.customer_id) * 5 * vi.original_price AS total_revenue_per_product +FROM ( + SELECT DISTINCT + vendor_id, + product_id, + original_price + FROM vendor_inventory +) vi +JOIN vendor v ON vi.vendor_id = v.vendor_id +JOIN product p ON vi.product_id = p.product_id +CROSS JOIN customer c +GROUP BY v.vendor_id, v.vendor_name, p.product_id, p.product_name, vi.original_price +ORDER BY v.vendor_name, p.product_name; -- INSERT /*1. Create a new table "product_units". @@ -99,12 +211,57 @@ 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(11) NOT NULL, + product_name VARCHAR(45) DEFAULT NULL, + product_size VARCHAR(45) DEFAULT NULL, + product_category_id INT(11) NOT NULL, + product_qty_type VARCHAR(45) DEFAULT NULL, + snapshot_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (product_id, snapshot_timestamp) +); + +INSERT INTO product_units ( + product_id, + product_name, + product_size, + product_category_id, + product_qty_type, + snapshot_timestamp +) +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'; +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 ( + product_id, + product_name, + product_size, + product_category_id, + product_qty_type, + snapshot_timestamp +) +VALUES ( + 999, -- product_id (use a unique ID) + 'Apple Pie', -- product_name + 'Large', -- product_size + 1, -- product_category_id (adjust to valid category) + 'unit', -- product_qty_type + CURRENT_TIMESTAMP -- snapshot_timestamp (current time) +); +SELECT * FROM product_units; -- DELETE /* 1. Delete the older record for the whatever product you added. @@ -113,6 +270,7 @@ HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/ + -- 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. @@ -130,6 +288,24 @@ 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; - +-- Step 2: Update with last quantity per product +UPDATE product_units +SET current_quantity = ( + SELECT COALESCE(SUM(vi.quantity), 0) + FROM vendor_inventory vi + WHERE vi.product_id = product_units.product_id + AND vi.market_date = ( + SELECT MAX(vi2.market_date) + FROM vendor_inventory vi2 + WHERE vi2.product_id = product_units.product_id + ) +) +WHERE product_units.product_id IN ( + SELECT DISTINCT product_id + FROM vendor_inventory +); +SELECT * FROM product_units; \ No newline at end of file diff --git a/05_src/sql/farmersmarket.db b/05_src/sql/farmersmarket.db index 4720f2483..aba07d522 100644 Binary files a/05_src/sql/farmersmarket.db and b/05_src/sql/farmersmarket.db differ