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: 9 additions & 1 deletion 02_activities/assignments/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Steps to complete this part of the assignment:
- Write, within this markdown file, an answer to Prompt 3



### Design a Logical Model

#### Prompt 1
Expand All @@ -43,7 +44,12 @@ Additionally, include a date table.

There are several tools online you can use, I'd recommend [Draw.io](https://www.drawio.com/) or [LucidChart](https://www.lucidchart.com/pages/).


**HINT:** You do not need to create any data for this prompt. This is a logical model (ERD) only.

**HINT:** You do not need to create any data for this prompt. This is a conceptual model only.
# Created base bookstore schema with core tables


#### Prompt 2
We want to create employee shifts, splitting up the day into morning and evening. Add this to the ERD.
Expand All @@ -57,7 +63,9 @@ The store wants to keep customer addresses. Propose two architectures for the CU
Your answer...
```

***
# The Type 1 architecture is where the CUSTOMER_ADDRESS simply overwrites old information when a customer's address changes. This saves storage space but loses historical data.

# The Type 2 architecture is the model where the CUSTOMER_ADDRESS table retains historical address changes by creating new records with effective_date, end_date, and an is_current flag. This method preserves historical data, allowing us to know past addresses, but it requires more storage and slightly more complex queries (for example, finding the current address where is_current = TRUE or end_date IS NULL).

## Section 2:
You can start this section following *session 4*.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
180 changes: 180 additions & 0 deletions 02_activities/assignments/assignment2-db browser.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
--COALESCE
SELECT
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_full_description
FROM product;

--Windowed Functions
--Q1
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
FROM customer_purchases;

SELECT
customer_id,
market_date,
DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
FROM customer_purchases;

--Q2
WITH visit_ranking AS (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS rn
FROM customer_purchases
)
SELECT
customer_id,
market_date
FROM visit_ranking
WHERE rn = 1;

SELECT
customer_id,
product_id,
COUNT(*) OVER (PARTITION BY customer_id, product_id) AS purchase_count
FROM customer_purchases;

--String manipulation
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;

SELECT
product_name,
product_size
FROM product
WHERE product_size REGEXP '[0-9]';

-- UNION
WITH daily_sales AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date
),
ranked_sales AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS sales_rank_high,
RANK() OVER (ORDER BY total_sales ASC) AS sales_rank_low
FROM daily_sales
)

SELECT market_date, total_sales, 'Highest Sales' AS sales_type
FROM ranked_sales
WHERE sales_rank_high = 1

UNION

SELECT market_date, total_sales, 'Lowest Sales' AS sales_type
FROM ranked_sales
WHERE sales_rank_low = 1

ORDER BY total_sales DESC;



-- Cross Join
WITH product_vendor_info AS (
SELECT
v.vendor_name,
p.product_name,
p.product_id,
vi.vendor_id,
vi.original_price AS product_price
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_joined_sales AS (
SELECT
pvi.vendor_name,
pvi.product_name,
pvi.product_price,
c.customer_id,
5 AS quantity -- Each customer buys 5 of each product
FROM product_vendor_info pvi
CROSS JOIN customer c
)

SELECT
vendor_name,
product_name,
SUM(quantity * product_price) AS potential_revenue
FROM cross_joined_sales
GROUP BY vendor_name, product_name
ORDER BY vendor_name, product_name;


-- First drop table if it exists to avoid errors
DROP TABLE IF EXISTS product_units;

-- Then create the table
CREATE TABLE product_units AS
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';

-- 2. Insert a new unit product with current timestamp
INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
VALUES (
(SELECT COALESCE(MAX(product_id), 0) + 1 FROM product_units), -- Auto-increment ID
'Gourmet Apple Pie', -- New product name
'10 inch', -- Product size
(SELECT product_category_id FROM product WHERE product_name LIKE '%Pie%' LIMIT 1), -- Matching category
'unit', -- Quantity type
CURRENT_TIMESTAMP -- Current timestamp
);

-- DELETE
/* 1. Delete the older record for the whatever product you added. */
DELETE FROM product_units
WHERE product_id = (
SELECT product_id
FROM product_units
WHERE product_name = 'Gourmet Apple Pie'
ORDER BY snapshot_timestamp ASC
LIMIT 1
);

-- UPDATE
/* 1. Add current_quantity to product_units and update with last quantity from vendor_inventory */

-- First add the column
ALTER TABLE product_units
ADD current_quantity INT;

-- Then update with the last quantity values
UPDATE product_units
SET current_quantity = COALESCE(
(SELECT quantity
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
ORDER BY market_date DESC, vendor_id DESC
LIMIT 1),
0
);
Loading