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
10 changes: 8 additions & 2 deletions 02_activities/assignments/Cohort_8/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ 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...
```
Your answer

Option 1: Type 1
In this design, the CUSTOMER_ADDRESS table stores only the recent address. When a customer moves, the existing address record is updated and the old information is removed. This is a Type 1 Slowly Changing Dimension, as previous changes are not kept.


Option 2: Type 2
Here, the CUSTOMER_ADDRESS table keeps multiple address records for customers. When addresses change, a new row is added with showing if the address is still used or not. Here the past addresses are saved.

***

Expand Down
15 changes: 13 additions & 2 deletions 02_activities/assignments/Cohort_8/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

--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. */
/* 1.Write a query that returns all customer purchases of product IDs 4 and 9. */

SELECT *
FROM customer_purchases
WHERE customer_id IN (4, 9);
Expand All @@ -32,18 +35,19 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:


-- option 2

SELECT *,
(quantity*cost_to_customer_per_qty) AS price
From customer_purchases
WHERE customer_id BETWEEN 8 AND 10;



--CASE
/* 1. Products can be sold by the individual unit or by bulk measures like lbs. or oz.
Using the product table, write a query that outputs the product_id and product_name
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'
Expand All @@ -56,6 +60,7 @@ 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'
Expand All @@ -71,6 +76,7 @@ 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
vendor.vendor_id,
vendor.vendor_name,
Expand All @@ -88,6 +94,7 @@ ORDER by vendor.vendor_name, vb.market_date;
-- AGGREGATE
/* 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 vendor_booth_counts
FROM vendor_booth_assignments
Expand All @@ -99,6 +106,7 @@ 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 customer.customer_id,
customer.customer_first_name,
customer.customer_last_name,
Expand All @@ -125,6 +133,7 @@ When inserting the new vendor, you need to appropriately align the columns to be
-> To insert the new row use VALUES, specifying the value you want for each column:
VALUES(col1,col2,col3,col4,col5)
*/

CREATE TABLE temp.new_vendor_copy2 as
SELECT *
From vendor;
Expand All @@ -141,6 +150,7 @@ VALUES (10, 'Thomass Superfood Store', 'Fresh Focused store', 'Thomas', 'Rosenth
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,
Expand All @@ -155,6 +165,7 @@ 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,
sum (quantity * cost_to_customer_per_qty) as total_spent_apr_2022
from customer_purchases
Expand Down
87 changes: 83 additions & 4 deletions 02_activities/assignments/Cohort_8/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ 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
Expand All @@ -34,18 +39,40 @@ 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,
ROW_NUMBER () over (PARTITION by customer_id
ORDER by market_date) as visit_nu

From customer_purchases
ORDER 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,
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,
ROW_NUMBER () over (PARTITION by customer_id
ORDER by market_date DESC) as visit_nu

From customer_purchases
ORDER by customer_id, market_date;

/* 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,
quantity,
count(*) over (
PARTITION by customer_id, product_id
) as purchase_times
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".
Expand All @@ -59,11 +86,23 @@ 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 description
FROM product;

/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */


SELECT *
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 Down Expand Up @@ -99,20 +138,34 @@ 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_qty_type 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_qty_type, product_category_id, snapshot_timestamp)
VALUES
(10, 'Eggs', '1 dozen', 'unit', 6, 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 = 'Eggs'
AND snapshot_timestamp < (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Eggs'
);
-- 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.
Expand All @@ -130,6 +183,32 @@ 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;

SELECT
product_id,
COALESCE(MAX(quantity), 0) AS last_quantity
FROM vendor_inventory
GROUP BY product_id;


WITH last_quantity AS (
SELECT
product_id,
COALESCE(MAX(quantity), 0) AS last_quantity
FROM vendor_inventory
GROUP BY product_id
)
UPDATE product_units
SET current_quantity = (
SELECT last_quantity
FROM last_quantity
WHERE last_quantity.product_id = product_units.product_id
)
WHERE product_id IN (
SELECT product_id FROM last_quantity
);



Binary file modified 05_src/sql/farmersmarket.db
Binary file not shown.
46 changes: 46 additions & 0 deletions 05_src/sql/farmersmarket.sqbpro
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="farmersmarket.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="9176"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><table title="product_units" custom_title="0" dock_id="1" table="4,13:mainproduct_units"/><dock_state state="000000ff00000000fd00000001000000020000028900000323fc0100000001fb000000160064006f0063006b00420072006f00770073006500310100000000000002890000010b00ffffff000002890000000000000004000000040000000800000008fc00000000"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="booth" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="104"/><column index="2" value="125"/><column index="3" value="300"/><column index="4" value="83"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="customer_purchases" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="80"/><column index="2" value="73"/><column index="3" value="91"/><column index="4" value="91"/><column index="5" value="62"/><column index="6" value="184"/><column index="7" value="119"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="product" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="80"/><column index="2" value="206"/><column index="3" value="93"/><column index="4" value="145"/><column index="5" value="124"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="product_units" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="80"/><column index="2" value="206"/><column index="3" value="93"/><column index="4" value="144"/><column index="5" value="124"/><column index="6" value="145"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1*">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_qty_type (product_id, product_name, product_size, product_qty_type, product_category_id, snapshot_timestamp)
Values ('10', 'Eggs', '1 dozen', '6', 'unit', snapshot_timest);

INSERT INTO product_units
(product_id, product_name, product_size, product_qty_type, product_category_id, snapshot_timestamp)
VALUES
(10, 'Eggs', '1 dozen', 'unit', 6, CURRENT_TIMESTAMP);


ALTER TABLE product_units
ADD current_quantity INT;

SELECT
product_id,
COALESCE(MAX(quantity), 0) AS last_quantity
FROM vendor_inventory
GROUP BY product_id;


WITH last_quantity AS (
SELECT
product_id,
COALESCE(MAX(quantity), 0) AS last_quantity
FROM vendor_inventory
GROUP BY product_id
)
UPDATE product_units
SET current_quantity = (
SELECT last_quantity
FROM last_quantity
WHERE last_quantity.product_id = product_units.product_id
)
WHERE product_id IN (
SELECT product_id FROM last_quantity
);
</sql><current_tab id="0"/></tab_sql></sqlb_project>
Binary file added Assignment2_Section1_Prompts 1-2.pdf
Binary file not shown.