diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 01f991d02..7ecb22d8a 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -56,7 +56,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: For a given customer, if their address changes, the old address is overwritten by the new address. Thus, only current data is maintained. + +Type 2: For a given customer, if their address changes, the new address is added in a new row and the old address is kept. Columns denoting start date, end date, and current status are needed to separate out-of-date entries from current entries. ``` *** @@ -191,5 +193,11 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c ``` -Your thoughts... +The data that neural nets are trained on must come from somewhere, and often, they were compiled and categorized by people. Therefore, it is important to be aware of the social and cultural context that prevailed during the time and place that the training data were gathered in. This is because these contexts may bias the categorization process. + +One solution to this issue that the article proposes to this issue is to have moderators go in and re-label data that were categorized in an “offensive” or “unfair” way. While this may be a short-term solution, it is important to recognize that the moderators themselves may introduce their own normative biases into the training data during this procedure. Therefore, it is important to maintain a constant dialogue around the construction of training data for neural nets. + +Furthermore, many of the training datasets were constructed collaboratively by people around the world, who may have been doing so out of personal interest, for projects, or to make money. Hence, it is important to recognize the labour input that went into this process. This fact is particularly interesting when we consider the anxieties that many people have today with respect to having their jobs and their role in society replaced by AI. One wonders whether such fears are perhaps slightly overblown, if, at the end of the day, human input is still fundamentally necessary for AI to operate. + +In summary, as with disruptive technologies that have arisen in the past, human input is still necessary for operating AI and neural net tools, and the prospect of robot domination seems to be unrealistic, at least for the time being. ``` diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index f7515f625..c7353fa69 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -22,10 +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') || ')' +FROM product; --END QUERY @@ -40,10 +39,11 @@ 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 + *, + 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 @@ -52,10 +52,14 @@ 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 * +FROM ( + SELECT + *, + DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date ASC) AS visit_number + FROM customer_purchases +) AS ranked_visits +WHERE visit_number = 1; --END QUERY @@ -65,10 +69,11 @@ 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(*) AS total_customer_purchase_qty +FROM customer_purchases +GROUP BY product_id, customer_id; --END QUERY @@ -84,19 +89,24 @@ 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 + *, + CASE + WHEN product_name LIKE '%-%' THEN SUBSTR(product_name, 1, INSTR(product_name, '-') - 1) + END AS product_name_unhyphenated +FROM product; --END QUERY /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ --QUERY 6 - - - - +SELECT + *, + CASE + WHEN product_name LIKE '%-%' THEN SUBSTR(product_name, 1, INSTR(product_name, '-') - 1) + END AS product_name_unhyphenated +FROM product +WHERE product_size REGEXP '[0-9]'; --END QUERY @@ -110,10 +120,30 @@ 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 sales_volume AS ( + SELECT + market_date, + SUM(quantity) AS daily_sales + FROM customer_purchases + GROUP BY market_date +), +day_types AS ( + SELECT + market_date, + daily_sales, + CASE + WHEN daily_sales = MIN(daily_sales) OVER () THEN 'worst day' + WHEN daily_sales = MAX(daily_sales) OVER () THEN 'best day' + END AS day_type + FROM sales_volume +) +SELECT * +FROM day_types +WHERE day_type = 'worst day' +UNION +SELECT * +FROM day_types +WHERE day_type = 'best day'; --END QUERY @@ -131,10 +161,27 @@ 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 + vendor_products AS ( + SELECT DISTINCT vendor_id, product_id, original_price + FROM vendor_inventory + ), + -- Calculate revenue per vendor, product combination + vendor_revenue AS ( + SELECT vendor_id, product_id, SUM(original_price)*5 AS revenue + FROM vendor_products + CROSS JOIN customer + GROUP BY vendor_id, product_id + ) +SELECT + vendor_revenue.vendor_id, + vendor_revenue.product_id, + vendor_revenue.revenue, + vendor.vendor_name, + product.product_name +FROM vendor_revenue +LEFT JOIN vendor ON vendor_revenue.vendor_id = vendor.vendor_id +LEFT JOIN product ON vendor_revenue.product_id = product.product_id; --END QUERY @@ -144,20 +191,20 @@ 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 *, CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_qty_type = 'unit'; --END QUERY /*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 +SELECT *, CURRENT_TIMESTAMP +FROM product +WHERE product_name = 'Apple Pie'; --END QUERY @@ -166,10 +213,13 @@ 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 snapshot_timestamp = ( + SELECT MIN(snapshot_timestamp) + FROM product_units + WHERE product_name = 'Apple Pie' + ) AND + product_name = 'Apple Pie'; --END QUERY @@ -190,10 +240,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. */ --QUERY 12 +ALTER TABLE product_units +ADD current_quantity INT; - - - +-- Update +UPDATE product_units +SET current_quantity = COALESCE( + ( + SELECT quantity + FROM vendor_inventory + WHERE vendor_inventory.product_id = product_units.product_id + ORDER BY market_date DESC + LIMIT 1 + ), + 0 +); --END QUERY diff --git a/02_activities/assignments/DC_Cohort/section1_erd_prompt1.png b/02_activities/assignments/DC_Cohort/section1_erd_prompt1.png new file mode 100644 index 000000000..58b71503e Binary files /dev/null and b/02_activities/assignments/DC_Cohort/section1_erd_prompt1.png differ diff --git a/02_activities/assignments/DC_Cohort/section1_erd_prompt2.png b/02_activities/assignments/DC_Cohort/section1_erd_prompt2.png new file mode 100644 index 000000000..07f371f91 Binary files /dev/null and b/02_activities/assignments/DC_Cohort/section1_erd_prompt2.png differ