Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 02_activities/assignments/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ The store wants to keep customer addresses. Propose two architectures for the CU
```
Your answer...
```
--The tables are in the png diagram. Type 1 SCD and Type 2 is labelled. The difference between the two tables are the efficiency in updating the information. Table 1 will overwrite the data when used the UPDATE command while the Type 2 will keep the old data and add the new data using the INSERT command and change the end date for the address that was on the database.





***

Expand Down
238 changes: 235 additions & 3 deletions 02_activities/assignments/assignment2.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* ASSIGNMENT 2 */
/* SECTION 2 */

FULL NAME - MUBASHARA MALBARI



/* SECTION 2 */

-- COALESCE
/* 1. Our favourite manager wants a detailed long list of products, but is afraid of tables!
Expand All @@ -21,6 +26,13 @@ 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 || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product;





--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -33,17 +45,94 @@ each new market date for each customer, or select only the unique market dates p
HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */


---using row_number query
SELECT
customer_id,
product_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date
) AS visit_number
FROM
customer_purchases;


---using dense_rank query
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date
) AS visit_number
FROM (
SELECT DISTINCT customer_id, market_date
FROM customer_purchases
) AS unique_visits;






/* 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. */

--reverse query
SELECT
customer_id,
product_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM
customer_purchases


--subquery using CTE
WITH ranked_visits AS (
SELECT
customer_id,
product_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM
customer_purchases
)

SELECT
customer_id,
product_id,
market_date
FROM ranked_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,
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".
Expand All @@ -59,9 +148,34 @@ Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR w




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. */


--REGEXP

SELECT
product_name,
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.
Expand All @@ -74,6 +188,53 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */


--answer to 1)
DROP TABLE IF EXISTS temp_sales_by_date;

CREATE TEMP TABLE temp_sales_by_date AS
SELECT
market_date,
SUM(quantity*cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date;

--answer to 2)
DROP TABLE IF EXISTS temp_ranked_sales;

CREATE TEMP TABLE temp_ranked_sales 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 temp_sales_by_date;


--answer to 3)

SELECT
market_date,
total_sales,
'Best Day' AS sales_rank
FROM
temp_ranked_sales
WHERE best_rank = 1

UNION

SELECT
market_date,
total_sales,
'Worst Day' AS sales_rank
FROM
temp_ranked_sales
WHERE worst_rank = 1








/* SECTION 3 */
Expand All @@ -90,25 +251,71 @@ How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */


DROP TABLE IF EXISTS customer_count;
CREATE TEMP TABLE customer_count AS
SELECT
COUNT (*) AS total_customers
FROM customer;


SELECT
v.vendor_name,
p.product_name,
(5 * cc.total_customers * vi.original_price) AS total_revenue
FROM
vendor_inventory vi
JOIN product p ON vi.product_id = p.product_id
JOIN vendor v ON vi.vendor_id = v.vendor_id
CROSS JOIN customer_count cc
GROUP BY
v.vendor_name,
p.product_name,
cc.total_customers;



-- INSERT
/*1. Create a new table "product_units".
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 *,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE 1 = 0;

INSERT INTO product_units
SELECT *,
CURRENT_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
VALUES (24, 'Sweet Corn', '2 Ear', 1, '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 = 'Sweet Corn'
AND product_id != 24;


-- UPDATE
Expand All @@ -120,7 +327,7 @@ ADD current_quantity INT;

Then, using UPDATE, change the current_quantity equal to the last quantity value from the vendor_inventory details.

HINT: This one is pretty hard.
HINT: This one is pretty hard.
First, determine how to get the "last" quantity per product.
Second, coalesce null values to 0 (if you don't have null values, figure out how to rearrange your query so you do.)
Third, SET current_quantity = (...your select statement...), remembering that WHERE can only accommodate one column.
Expand All @@ -129,5 +336,30 @@ 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. */



---add column

ALTER TABLE product_units
ADD current_quantity INT;


--update statement

UPDATE product_units

--coalesce null values to 0
SET current_quantity = COALESCE((
SELECT quantity
FROM (
SELECT
vendor_inventory.product_id,
vendor_inventory.quantity,
ROW_NUMBER() OVER (PARTITION BY vendor_inventory.product_id
ORDER BY vendor_inventory.market_date DESC) AS row_num
FROM vendor_inventory
) AS ranked_inventory

--where statement
WHERE ranked_inventory.product_id = product_units.product_id
AND ranked_inventory.row_num = 1
), 0);

Binary file added 02_activities/assignments/conceptual ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 05_src/sql/farmersmarket.db-journal
Binary file not shown.