diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 47a854540..cc95047c7 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -48,8 +48,13 @@ There are several tools online you can use, I'd recommend [Draw.io](https://www. **HINT:** You do not need to create any data for this prompt. This is a conceptual model only. +**PROMPT 1 RESPONSE** + + #### Prompt 2 We want to create employee shifts, splitting up the day into morning and evening. Add this to the ERD. +**UPDATED ERD WITH EMPLOYEE SHIFTS** + #### Prompt 3 The store wants to keep customer addresses. Propose two architectures for the CUSTOMER_ADDRESS table, one that will retain changes, and another that will overwrite. Which is type 1, which is type 2? @@ -57,7 +62,25 @@ 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 SCDs: +- overwrite old data with new data +- use if you do not need to keep track of old data +- saves storage space! + +customer_address (type 1) +columns: customer_id, street_address, city, province, postal_code + +Once a change is applied, old data is lost! + +Type 2 SCDs: +- preserves old data by creating new records for each change +- e.g., if we want to understand migration patterns + +customer_address (type 2) +columns: address_id, customer_id, street_address, city, province, postal_code, effective_date, end_date, is_current + +Current and previous addresses are stored under unique address_id for each customer. is_current is a binary column reflecting whether the address_id in question is current or not. + ``` *** @@ -192,5 +215,13 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c ``` -Your thoughts... +Some ethical issues that stood out to me in this piece are: + +1. Invisible labour and exploitation of graduate students and other people around the globe hired to complete tedious tasks (e.g., tagging countless images) for a few cents. AI, machinee learning, and neural nets heavily depend on the work of people who collect, label, and classify data. This hard work is rarely recognized and compensation for the work rarely reflects the value the work ultimately generates. For example, AI is often presented as something fully automated but this obscures the efforts of the people and organizations responsible for its outputs. On a similar note, it becomes hard to determine who is to be held accountable when AI makes harmful decisions. + +2. Subjectivity and bias (cultaral, societal, personal biases) of the persons carrying out the tedious work of labeling and classifying the training data can become embedded in AI systems. The section on ImageNet Roulette clearly demonstrates how such biases and sometimes even offensive behaviours can become a part of how an AI system behaves. + +Ultimately, the article emphasizes that AI is not independent of society; it reflects the decisions, labour, and values of the people involved in its development. Greater transparency and ethical oversight are therefore necessary to ensure that technological progress does not come at the expense of fairness and human well-being. + +3. ``` diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index 4079c18ae..72dd7c472 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -23,7 +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 @@ -41,7 +45,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 ASC + ) AS visit_number +FROM customer_purchases +WHERE market_date < '2022-04-29'; --END QUERY @@ -53,8 +65,18 @@ only the customer’s most recent visit. HINT: Do not use the previous visit dates filter. */ --QUERY 3 - - +SELECT * +FROM ( + SELECT DISTINCT + customer_id, + market_date, + DENSE_RANK() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number + FROM customer_purchases +) +WHERE visit_number = 1; --END QUERY @@ -66,7 +88,12 @@ 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 *, + COUNT(*) OVER ( + PARTITION BY customer_id, product_id + ) AS purchase_count +FROM customer_purchases +WHERE market_date < '2022-04-29'; --END QUERY @@ -85,7 +112,15 @@ 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, + TRIM( + SUBSTR( + product_name, + NULLIF(INSTR(product_name, '-'), 0) + 1 + ) + ) AS description +FROM product; --END QUERY @@ -94,7 +129,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 @@ -111,7 +148,32 @@ 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 day_sales 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_day, + RANK() OVER (ORDER BY total_sales ASC) AS worst_day + FROM day_sales +) + +SELECT market_date, total_sales +FROM ranked_sales +WHERE best_day = 1 +UNION + +SELECT market_date, total_sales +FROM ranked_sales +WHERE worst_day = 1; --END QUERY @@ -132,8 +194,26 @@ 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 * vinv.original_price AS total_revenue +FROM ( + SELECT DISTINCT + vendor_id, + product_id, + original_price + FROM vendor_inventory +) AS vinv +INNER JOIN vendor as v + ON vinv.vendor_id = v.vendor_id +INNER JOIN product AS p + ON vinv.product_id = p.product_id +CROSS JOIN customer as c +GROUP BY + v.vendor_id, + p.product_id, + vinv.original_price; --END QUERY @@ -145,8 +225,19 @@ 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'; +/* +to check new table: +SELECT * +FROM product_units; +/* --END QUERY @@ -155,7 +246,16 @@ 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 +SELECT + product.*, + CURRENT_TIMESTAMP +FROM product +WHERE product_name = 'Apple Pie'; +SELECT * +FROM product_units +WHERE product_name = 'Apple Pie'; --END QUERY @@ -167,8 +267,18 @@ 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 MIN(snapshot_timestamp) + 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 @@ -191,8 +301,27 @@ 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 vinv.quantity + FROM vendor_inventory AS vinv + WHERE vinv.product_id = product_units.product_id + ORDER BY vinv.market_date DESC + LIMIT 1 +), 0); + +/* +-- check query + +SELECT + product_id, + product_name, + current_quantity +FROM product_units; +/* --END QUERY diff --git a/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM.png b/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM.png new file mode 100644 index 000000000..9e0271f8c Binary files /dev/null and b/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM.png differ diff --git a/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM_shifts.png b/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM_shifts.png new file mode 100644 index 000000000..00786128a Binary files /dev/null and b/02_activities/assignments/DC_Cohort/images/02_bookstore_LDM_shifts.png differ