diff --git a/02_activities/assignments/Assignment2.md b/02_activities/assignments/Assignment2.md index 05244623b..2abe69060 100644 --- a/02_activities/assignments/Assignment2.md +++ b/02_activities/assignments/Assignment2.md @@ -53,8 +53,9 @@ 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... +By adding a "customer address" table with three columns, we can: +1- use an FK to connect with customer_id from the "customer" table, thus overwriting if needed. +2- store old and new customer addresses in new columns. ``` *** diff --git a/02_activities/assignments/assigment1_data_model.jpg b/02_activities/assignments/assigment1_data_model.jpg new file mode 100644 index 000000000..440009ee2 Binary files /dev/null and b/02_activities/assignments/assigment1_data_model.jpg differ diff --git a/02_activities/assignments/assignment1.sql b/02_activities/assignments/assignment1.sql index 2e89fa7af..92790fe7a 100644 --- a/02_activities/assignments/assignment1.sql +++ b/02_activities/assignments/assignment1.sql @@ -4,21 +4,29 @@ --SELECT /* 1. Write a query that returns everything in the customer table. */ - - +SELECT * FROM customer; /* 2. Write a query that displays all of the columns and 10 rows from the cus- tomer table, sorted by customer_last_name, then customer_first_ name. */ - +SELECT * FROM customer +ORDER BY customer_last_name, customer_first_name +LIMIT 10; --WHERE /* 1. Write a query that returns all customer purchases of product IDs 4 and 9. */ -- option 1 - +SELECT * +FROM customer_purchases +WHERE + product_id = 4 or product_id = 9; + -- option 2 - +SELECT * +FROM customer_purchases +WHERE + product_id IN (4, 9); /*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty), @@ -27,10 +35,22 @@ filtered by vendor IDs between 8 and 10 (inclusive) using either: 2. one condition using BETWEEN */ -- option 1 +SELECT * +,(quantity * cost_to_customer_per_qty) AS price +FROM + customer_purchases cp +WHERE + cp.vendor_id >= 8 + AND cp.vendor_id <= 10; -- option 2 - +SELECT * +,(quantity * cost_to_customer_per_qty) AS price +FROM + customer_purchases cp +WHERE + cp.vendor_id BETWEEN 8 AND 10; --CASE @@ -39,19 +59,45 @@ Using the product table, write a query that outputs the product_id and product_n columns and add a column called prod_qty_type_condensed that displays the word “unit” if the product_qty_type is “unit,” and otherwise displays the word “bulk.” */ +SELECT product_id, product_name +,CASE +When product_qty_type = 'unit' then 'unit' +When product_qty_type in ('lbs', 'oz') then 'bulk' +When product_qty_type is null THEN NULL +ELSE NULL +END as prod_qty_type_condensed + +FROM product /* 2. We want to flag all of the different types of pepper products that are sold at the market. add a column to the previous query called pepper_flag that outputs a 1 if the product_name contains the word “pepper” (regardless of capitalization), and otherwise outputs 0. */ - +SELECT product_id, product_name +,CASE +When product_qty_type = 'unit' then 'unit' +When product_qty_type in ('lbs', 'oz') then 'bulk' +When product_qty_type is null THEN NULL +ELSE NULL +END as prod_qty_type_condensed +, CASE +when lower (product_name) like '%pepper%' THEN 1 +ELSE 0 +END AS PEPPER_FLAD +FROM product --JOIN /* 1. Write a query that INNER JOINs the vendor table to the vendor_booth_assignments table on the vendor_id field they both have in common, and sorts the result by vendor_name, then market_date. */ - +SELECT * +FROM + vendor v +INNER JOIN + vendor_booth_assignments vba ON v.vendor_id = vba.vendor_id +ORDER BY + v.vendor_name, vba.market_date; /* SECTION 3 */ @@ -60,7 +106,12 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t /* 1. Write a query that determines how many times each vendor has rented a booth at the farmer’s market by counting the vendor booth assignments per vendor_id. */ - +SELECT + vendor_id + ,COUNT(*) AS booth_rented +FROM +vendor_booth_assignments +GROUP by 1; /* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper sticker to everyone who has ever spent more than $2000 at the market. Write a query that generates a list @@ -68,6 +119,22 @@ of customers for them to give stickers to, sorted by last name, then first name. HINT: This query requires you to join two tables, use an aggregate function, and use the HAVING keyword. */ +SELECT +c.customer_first_name +,c.customer_last_name + +FROM ( +SELECT customer_id +, sum ((quantity * cost_to_customer_per_qty)) as total_spent + +FROM customer_purchases +GROUP by 1 +HAVING total_spent > 2000 +) TS + +left JOIN customer c +on ts.customer_id = c.customer_id +ORDER by customer_first_name, customer_last_name; --Temp Table @@ -82,7 +149,16 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ +CREATE TEMP TABLE new_vendor AS + +SELECT * +from vendor; + +INSERT into new_vendor +VALUES (10, "Thomass Superfood store", "'a Fresh Focused store", "Thomas", "Rosenthal"); +SELECT * +FROM new_vendor; -- Date /*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table. @@ -90,6 +166,11 @@ VALUES(col1,col2,col3,col4,col5) HINT: you might need to search for strfrtime modifers sqlite on the web to know what the modifers for month and year are! */ +SELECT +customer_id +, strftime ('%m', market_date) as month +, strftime ('%Y', market_date) as year +from customer_purchases; /* 2. Using the previous query as a base, determine how much money each customer spent in April 2022. @@ -98,3 +179,18 @@ Remember that money spent is quantity*cost_to_customer_per_qty. HINTS: you will need to AGGREGATE, GROUP BY, and filter... but remember, STRFTIME returns a STRING for your WHERE statement!! */ +SELECT +customer_id +, month +, year +, sum (price) as monthly_spent + +FROM ( +SELECT customer_id +, strftime ('%m', market_date) as month +, strftime ('%Y', market_date) as year +, (quantity * cost_to_customer_per_qty) as price +from customer_purchases +) tl + +GROUP by 1,2,3; \ No newline at end of file diff --git a/02_activities/assignments/assignment2.sql b/02_activities/assignments/assignment2.sql index 5ad40748a..9bef46194 100644 --- a/02_activities/assignments/assignment2.sql +++ b/02_activities/assignments/assignment2.sql @@ -21,6 +21,16 @@ Edit the appropriate columns -- you're making two edits -- and the NULL rows wil All the other rows will remain the same.) */ +SELECT +product_name || ', ' || product_size|| ' (' || product_qty_type || ')' +FROM product; +SELECT + product_name || ', ' || + COALESCE(product_size, '') || ' (' || + COALESCE(product_qty_type, 'unit') || ')' AS detailed_product_info +FROM product; + + --Windowed Functions /* 1. Write a query that selects from the customer_purchases table and numbers each customer’s @@ -32,6 +42,16 @@ 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) AS visit_number +FROM + customer_purchases +GROUP BY + customer_id, + market_date; + /* 2. Reverse the numbering of the query from a part so each customer’s most recent visit is labeled 1, @@ -39,11 +59,29 @@ then write another query that uses this one as a subquery (or temp table) and fi only the customer’s most recent visit. */ +WITH CustomerVisits AS ( + SELECT + customer_id, + market_date, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number + FROM + customer_purchases +) +SELECT + customer_id, + market_date +FROM + CustomerVisits +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 product_purchase_count +FROM + customer_purchases; -- String manipulations /* 1. Some product names in the product table have descriptions like "Jar" or "Organic". @@ -57,10 +95,18 @@ 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, + TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) AS description +FROM + product; /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ +SELECT product_name, product_size +FROM product +WHERE product_size REGEXP '[0-9]'; -- UNION @@ -74,6 +120,34 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling 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, + 'Highest Sales Day' AS description +FROM RankedSales +WHERE rank_highest = 1 +UNION +SELECT + market_date, + total_sales, + 'Lowest Sales Day' AS description +FROM RankedSales +WHERE rank_lowest = 1; /* SECTION 3 */ @@ -89,6 +163,17 @@ 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 vendor_customer_cross AS ( + SELECT vendor.vendor_id, vendor.vendor_name, customer.customer_id + FROM vendor CROSS JOIN customer +),vendor_products AS ( + SELECT vi.vendor_id, p.product_name, vi.original_price FROM vendor_inventory vi + JOIN product p ON vi.product_id = p.product_id) +SELECT vc.vendor_name, + vp.product_name, SUM(5 * vp.original_price) AS total_earnings +FROM vendor_customer_cross vcJOIN vendor_products vp ON vc.vendor_id = vp.vendor_id +GROUP BY vc.vendor_name, vp.product_nameORDER BY vc.vendor_name, vp.product_name; + -- INSERT @@ -97,18 +182,47 @@ 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 + *, + 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 ( + 999, + 'Apple Pie', + 'large', + 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 product_name = 'Apple Pie' + AND snapshot_timestamp = ( + SELECT MIN(snapshot_timestamp) + FROM product_units + WHERE product_name = 'Apple Pie' + ); + -- UPDATE diff --git a/05_src/sql/farmersmarket.db-journal b/05_src/sql/farmersmarket.db-journal new file mode 100644 index 000000000..318b10b4b Binary files /dev/null and b/05_src/sql/farmersmarket.db-journal differ diff --git a/bookstore_diloreto.jpg b/bookstore_diloreto.jpg new file mode 100644 index 000000000..555a6845a Binary files /dev/null and b/bookstore_diloreto.jpg differ