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
25 changes: 23 additions & 2 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,24 @@ 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...
Type 1: When the customer address changes, change the address, and do not keep the old one.

It would be enough to store:
customer_id
and adress(street, city, province, postal_code, country)


Type 2: Each time the customer changes the address, add another row. This keeps the previous addresses.

We would need to store:

address_id
customer_id
adress(street, city, province, postal_code, country)
effective_date
end_date(when the address changes)
is_current (yes or no)

```

***
Expand Down Expand Up @@ -193,5 +210,9 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c


```
Your thoughts...
-Huge profits are made out of the labour of workers. Yet, this is not transparently acknowledged, though this is not unexpected in the capitalism. Although this is an important issue, this is not distinctly different than how -in any area- the labour is treated in capitalism.
-Because the categorizations are created by humans, the biases that humans have are reflected on the datasets.
-AI is not seperate from the human values.


```
140 changes: 125 additions & 15 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ Edit the appropriate columns -- you're making two edits -- and the NULL rows wil
All the other rows will remain the same. */
--QUERY 1



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

--END QUERY

Expand All @@ -41,8 +44,15 @@ HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK().
Filter the visits to dates before April 29, 2022. */
--QUERY 2



SELECT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date) AS visit_number
FROM customer_purchases
WHERE market_date < '2022-04-29'
ORDER BY customer_id, market_date;

--END QUERY

Expand All @@ -53,7 +63,13 @@ only the customer’s most recent visit.
HINT: Do not use the previous visit dates filter. */
--QUERY 3


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


--END QUERY
Expand All @@ -66,7 +82,16 @@ You can make this a running count by including an ORDER BY within the PARTITION
Filter the visits to dates before April 29, 2022. */
--QUERY 4


SELECT
customer_id,
market_date,
product_id,
quantity,
COUNT(*) OVER (
PARTITION BY customer_id, product_id) AS times_purchased
FROM customer_purchases
WHERE market_date < '2022-04-29'
ORDER BY customer_id, product_id, market_date;


--END QUERY
Expand All @@ -85,7 +110,14 @@ 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. */
--QUERY 5


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;


--END QUERY
Expand All @@ -94,7 +126,9 @@ Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR w
/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */
--QUERY 6


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


--END QUERY
Expand All @@ -111,9 +145,37 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */
--QUERY 7




WITH sales_by_date AS (
SELECT
market_date,
SUM(quantity * cost_per_quantity) 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 best_rank,
RANK() OVER (ORDER BY total_sales ASC) AS worst_rank
FROM sales_by_date
)

SELECT
market_date,
total_sales,
'Highest Sales' AS sales_day
FROM ranked_sales
WHERE best_rank = 1

UNION

SELECT
market_date,
total_sales,
'Lowest Sales' AS sales_day
FROM ranked_sales
WHERE worst_rank = 1;
--END QUERY


Expand All @@ -132,7 +194,23 @@ How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */
--QUERY 8


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


--END QUERY
Expand All @@ -145,7 +223,12 @@ It should use all of the columns from the product table, as well as a new column
Name the timestamp column `snapshot_timestamp`. */
--QUERY 9


CREATE TABLE product_units AS
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';


--END QUERY
Expand All @@ -155,7 +238,19 @@ Name the timestamp column `snapshot_timestamp`. */
This can be any product you desire (e.g. add another record for Apple Pie). */
--QUERY 10


INSERT INTO product_units (
product_id,
product_name,
product_size,
product_qty_type,
snapshot_timestamp
)
VALUES (
999,
'Apple Pie',
'1',
'unit',
CURRENT_TIMESTAMP);


--END QUERY
Expand All @@ -167,7 +262,12 @@ This can be any product you desire (e.g. add another record for Apple Pie). */
HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/
--QUERY 11


DELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp <> (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Apple Pie');


--END QUERY
Expand All @@ -191,7 +291,17 @@ 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. */
--QUERY 12

ALTER TABLE product_units
ADD current_quantity INT;

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


--END QUERY
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