diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 72f8c71fd..2de5a44fb 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -57,7 +57,9 @@ 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 — overwrite the address: A CUSTOMER_ADDRESS table could contain customer_id, street, city, province, and postal_code. When the customer moves to a new address, the existing row is updated with the new address. The table always keeps the current address while previous addresses are deleted. + +Type 2 — retain address history: The CUSTOMER_ADDRESS table could contain address_version_id (e.g. 0, 1, 2,...etc) as its primary key, customer_id as a foreign key, the address fields, and valid_from and valid_to dates. When the customer's address changes, the existing row is closed by filling in valid_to column, and a new row is added with the new address along with valid_from column filled in. ``` *** @@ -193,5 +195,13 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c ``` -Your thoughts... +The article made me think about the people whose work makes machine learning possible, particularly those who prepare and label training data. The article describes how ImageNet relied on people working through Amazon Mechanical Turk to identify images. Although these workers contributed to a dataset that became the foundation of AI training, their individual contributions are much less recognized than the resulting technology. + +One ethical concern is whether workers are compensated fairly. Labeling a single image may seem like a small task, but building a large dataset requires hundreds of thousands of decisions. Pay should reflect not only the time spent clicking and choosing labels, but also the time needed to understand instructions, resolve uncertainty, and check work. If workers are paid only for completed tasks, their effective hourly earnings may be lower than they first appear. + +Working conditions matter as well. Repetitive tasks need sustained attention, and some datasets may expose workers to disturbing content. Organizations should consider reasonable workloads, clear instructions, opportunities for breaks, and appropriate support when sensitive content is involved. + +Another issue is the unequal distribution of recognition and benefits. Researchers and tech companies receive professional rewards or make huge profits from a successful system, while the people who helped build its training databases remain anonymous. Publicly describing their contribution and the conditions under which the work was done would make this dependence more visible. + +For me, the article challenges the idea that AI is simply the product of complex algorithms. It also depends on people performing detailed and repetitive tasks. Treating that labor as essential means considering fair pay, safe conditions, and meaningful recognition as part of responsible AI development. ``` diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index 4079c18ae..de6ca6d01 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -22,9 +22,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. */ --QUERY 1 - - - +SELECT product_name || ', ' || COALESCE(product_size, '') || ' (' || + COALESCE(product_qty_type, 'unit') || ')' AS product_details +FROM product; --END QUERY @@ -40,9 +40,10 @@ each new market date for each customer, or select only the unique market dates p 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, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number +FROM (SELECT DISTINCT customer_id, market_date FROM customer_purchases + WHERE market_date < '2022-04-29'); --END QUERY @@ -52,9 +53,17 @@ then write another query that uses this one as a subquery (or temp table) and fi only the customer’s most recent visit. HINT: Do not use the previous visit dates filter. */ --QUERY 3 - - - +SELECT customer_id, market_date, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number +FROM (SELECT DISTINCT customer_id, market_date FROM customer_purchases); + +SELECT customer_id, market_date, visit_number +FROM ( + SELECT customer_id, market_date, + ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number + FROM (SELECT DISTINCT customer_id, market_date FROM customer_purchases) +) +WHERE visit_number = 1; --END QUERY @@ -65,9 +74,9 @@ customer_purchases table that indicates how many different times that customer h You can make this a running count by including an ORDER BY within the PARTITION BY if desired. 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 @@ -84,18 +93,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. */ --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 /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ --QUERY 6 - - - +SELECT product_name, product_size, + CASE WHEN INSTR(product_name, '-') > 0 + THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) + ELSE NULL END AS description +FROM product +WHERE product_size REGEXP '[0-9]'; --END QUERY @@ -110,9 +124,21 @@ 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. */ --QUERY 7 - - - +WITH daily_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 highest_rank, + RANK() OVER (ORDER BY total_sales ASC) AS lowest_rank + FROM daily_sales +) +SELECT market_date, total_sales, 'highest' AS sales_type +FROM ranked_sales WHERE highest_rank = 1 +UNION +SELECT market_date, total_sales, 'lowest' AS sales_type +FROM ranked_sales WHERE lowest_rank = 1; --END QUERY @@ -131,9 +157,24 @@ 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). */ --QUERY 8 - - - +WITH ranked_inventory AS ( + SELECT vendor_id, product_id, original_price, + ROW_NUMBER() OVER ( + PARTITION BY vendor_id, product_id ORDER BY market_date DESC + ) AS price_rank + FROM vendor_inventory +), vendor_products AS ( + SELECT vendor_id, product_id, original_price + FROM ranked_inventory + WHERE price_rank = 1 +) +SELECT v.vendor_name, p.product_name, + SUM(5 * vp.original_price) AS potential_sales +FROM vendor_products AS vp +CROSS JOIN customer AS c +JOIN vendor AS v ON v.vendor_id = vp.vendor_id +JOIN product AS p ON p.product_id = vp.product_id +GROUP BY vp.vendor_id, vp.product_id, v.vendor_name, p.product_name; --END QUERY @@ -144,9 +185,10 @@ 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`. */ --QUERY 9 - - - +CREATE TABLE product_units AS +SELECT product.*, CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_qty_type = 'unit'; --END QUERY @@ -154,9 +196,11 @@ Name the timestamp column `snapshot_timestamp`. */ /*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). */ --QUERY 10 - - - +INSERT INTO product_units + (product_id, product_name, product_size, product_category_id, product_qty_type, snapshot_timestamp) +SELECT product_id, product_name, product_size, product_category_id, product_qty_type, CURRENT_TIMESTAMP +FROM product +WHERE product_name = 'Apple Pie'; --END QUERY @@ -166,9 +210,14 @@ 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 - - - +-- rowid breaks ties if both snapshots were created in the same second. +DELETE FROM product_units +WHERE rowid = ( + SELECT rowid FROM product_units + WHERE product_name = 'Apple Pie' + ORDER BY snapshot_timestamp ASC, rowid ASC + LIMIT 1 +); --END QUERY @@ -190,9 +239,17 @@ 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. */ --QUERY 12 - - - +ALTER TABLE product_units ADD current_quantity INT; + +-- If more than one vendor has inventory on the latest date, use the lowest vendor ID. +UPDATE product_units +SET current_quantity = COALESCE(( + SELECT vi.quantity + FROM vendor_inventory AS vi + WHERE vi.product_id = product_units.product_id + ORDER BY vi.market_date DESC, vi.vendor_id ASC + LIMIT 1 +), 0); --END QUERY diff --git a/02_activities/assignments/DC_Cohort/assignment2_bookstore.drawio.pdf b/02_activities/assignments/DC_Cohort/assignment2_bookstore.drawio.pdf new file mode 100644 index 000000000..4e085c595 Binary files /dev/null and b/02_activities/assignments/DC_Cohort/assignment2_bookstore.drawio.pdf differ