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
104 changes: 100 additions & 4 deletions 02_activities/assignments/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,106 @@ The store wants to keep customer addresses. Propose two architectures for the CU

**HINT:** search type 1 vs type 2 slowly changing dimensions.

Overwrite Model is Type 1 and Retain Changes is Type 2
Overwrite Model is to replace existing model and Retain Changes is keeping and updating existing system.

```
Your answer...
Model for a Small Bookstore

Employee
employee_id
first_name
last_name
position
hire_date
salary

Customer
customer_id
first_name
last_name
email
phone

Book
book_id
title
author
ISBN
genre
price
publication_date

Order
order_id (PK)
customer_id (FK → Customer.customer_id)
employee_id (FK → Employee.employee_id)
order_date (FK → Date.date_id)
total_amount

Order_Detail
order_detail_id (PK)
order_id (FK → Order.order_id)
book_id (FK → Book.book_id)
quantity
unit_price

Sales
sale_id (PK)
order_id (FK → Order.order_id)
payment_date (FK → Date.date_id)
payment_type
amount

Date
date_id (PK)
calendar_date
year
month
quarter
weekday
is_holiday

Relationships:
One customer can place many orders.
One order can have many order_details.
One book can appear in many order_details.
One employee processes many orders.
Date connects to both orders and sales.

Adding Employee Shifts with Morning & Evening Shifts:
Shift
shift_id (PK)
shift_name
start_time
end_time

Employee_Shift
employee_id (FK → Employee.employee_id)
shift_id (FK → Shift.shift_id)
date_id (FK → Date.date_id)

Overwrite Model
customer_id (FK → Customer.customer_id)
street
city
province
postal_code
country

History Retention Model
customer_address_id (PK)
customer_id (FK → Customer.customer_id)
street
city
province
postal_code
country
effective_date
end_date
is_current

Overwrite Model is Type 1 and Retain Changes is Type 2
```

***
Expand All @@ -76,9 +174,7 @@ Steps to complete this part of the assignment:

Using the following syntax you create our super cool and not at all needy manager a list:
```
SELECT
product_name || ', ' || product_size|| ' (' || product_qty_type || ')'
FROM product

```

But wait! The product table has some bad data (a few NULL values).
Expand Down
112 changes: 112 additions & 0 deletions 02_activities/assignments/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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.) */

SELECT
COALESCE(product_name, '') || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_details
FROM product;


--Windowed Functions
Expand All @@ -32,17 +35,37 @@ 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_number
FROM customer_purchases;


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

WITH ranked AS (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS rev_visit_number
FROM customer_purchases
)
SELECT *
FROM ranked
WHERE rev_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,
COUNT(*) OVER (PARTITION BY customer_id, product_id) AS product_purchase_count
FROM customer_purchases;


-- String manipulations
Expand All @@ -57,10 +80,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
WHERE INSTR(product_name, '-') > 0;


/* 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
Expand All @@ -73,6 +104,33 @@ 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. */

WITH sales_per_day AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date
),
ranked 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 sales_per_day
)
-- Best Day
SELECT market_date, total_sales, 'Best Day' AS label
FROM ranked
WHERE best_rank = 1

UNION

-- Worst Day
SELECT market_date, total_sales, 'Worst Day' AS label
FROM ranked
WHERE worst_rank = 1;




Expand All @@ -89,6 +147,31 @@ 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 customer_count AS (
SELECT COUNT(*) AS num_customers
FROM customer
)
, vendor_products AS (
SELECT
v.vendor_id,
v.vendor_name,
p.product_id,
p.product_name,
vi.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
GROUP BY v.vendor_id, v.vendor_name, p.product_id, p.product_name, vi.original_price
)

SELECT
vp.vendor_name,
vp.product_name,
SUM(5 * vp.original_price) AS total_revenue_per_product
FROM vendor_products vp
CROSS JOIN customer_count cc
GROUP BY vp.vendor_name, vp.product_name
ORDER BY vp.vendor_name, vp.product_name;


-- INSERT
Expand All @@ -97,18 +180,32 @@ 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 p.*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product p
WHERE p.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
SELECT *, CURRENT_TIMESTAMP
FROM product
WHERE product_name = 'Apple Pie';


-- 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
Expand All @@ -128,6 +225,21 @@ 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 COLUMN current_quantity INTEGER DEFAULT 0;

UPDATE product_units
SET current_quantity = (
SELECT COALESCE(vi.quantity, 0)
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1
)
WHERE EXISTS (
SELECT 1
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
);



2 changes: 1 addition & 1 deletion 02_activities/assignments/farmersmarket.sqbpro
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/Asad/dsi_day2/SQL1/05_src/sql/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="8651"/><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="vendor" custom_title="0" dock_id="3" table="4,6:mainvendor"/><dock_state state="000000ff00000000fd00000001000000020000043b000002b6fc0100000003fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000003480000000000000000fb000000160064006f0063006b00420072006f007700730065003301000000000000043b0000011800ffffff000002700000000000000004000000040000000800000008fc00000000"/><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="90"/><column index="2" value="108"/><column index="3" value="300"/><column index="4" value="72"/></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="69"/><column index="2" value="64"/><column index="3" value="85"/><column index="4" value="79"/><column index="5" value="55"/><column index="6" value="158"/><column index="7" value="103"/></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="69"/><column index="2" value="296"/><column index="3" value="117"/><column index="4" value="126"/><column index="5" value="107"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="64"/><column index="2" value="257"/><column index="3" value="234"/><column index="4" value="156"/><column index="5" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor_booth_assignments" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="66"/><column index="2" value="92"/><column index="3" value="85"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor_inventory" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="85"/><column index="2" value="55"/><column index="3" value="66"/><column index="4" value="71"/><column index="5" value="85"/></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="assignment1.sql" filename="C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment1.sql">-- Reference to file &quot;C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment1.sql&quot; (not supported by this version) --</sql><current_tab id="0"/></tab_sql></sqlb_project>
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/Asad/dsi_day2/SQL1/05_src/sql/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="8651"/><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"/><expanded_item id="4" parent="1"/></tab_structure><tab_browse><table title="product" custom_title="0" dock_id="3" table="4,7:mainproduct"/><dock_state state="000000ff00000000fd00000001000000020000043b000002b6fc0100000003fb000000160064006f0063006b00420072006f00770073006500310100000000ffffffff0000000000000000fb000000160064006f0063006b00420072006f00770073006500320100000000000003480000000000000000fb000000160064006f0063006b00420072006f007700730065003301000000000000043b0000011e00ffffff000002700000000000000004000000040000000800000008fc00000000"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="assignment2" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="300"/><column index="2" value="300"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><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="90"/><column index="2" value="108"/><column index="3" value="300"/><column index="4" value="72"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="customer" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="77"/><column index="2" value="127"/><column index="3" value="125"/><column index="4" value="136"/></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="69"/><column index="2" value="64"/><column index="3" value="85"/><column index="4" value="79"/><column index="5" value="55"/><column index="6" value="158"/><column index="7" value="103"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="market_date_info" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="85"/><column index="2" value="78"/><column index="3" value="85"/><column index="4" value="79"/><column index="5" value="113"/><column index="6" value="107"/><column index="7" value="84"/><column index="8" value="140"/><column index="9" value="111"/><column index="10" value="114"/><column index="11" value="105"/><column index="12" value="112"/></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="69"/><column index="2" value="296"/><column index="3" value="117"/><column index="4" value="126"/><column index="5" value="107"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="product_category" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="124"/><column index="2" value="234"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="temp" name="new_vendor" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="64"/><column index="2" value="257"/><column index="3" value="234"/><column index="4" value="156"/><column index="5" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="64"/><column index="2" value="257"/><column index="3" value="234"/><column index="4" value="156"/><column index="5" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor_booth_assignments" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="66"/><column index="2" value="92"/><column index="3" value="85"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="vendor_inventory" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_" freeze_columns="0"><sort/><column_widths><column index="1" value="85"/><column index="2" value="55"/><column index="3" value="66"/><column index="4" value="71"/><column index="5" value="85"/></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="assignment1.sql" filename="C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment1.sql">-- Reference to file &quot;C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment1.sql&quot; (not supported by this version) --</sql><sql name="assignment2.sql*" filename="C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment2.sql">-- Reference to file &quot;C:/Users/Asad/dsi_day2/SQL1/02_activities/assignments/assignment2.sql&quot; (not supported by this version) --</sql><current_tab id="1"/></tab_sql></sqlb_project>