diff --git a/02_activities/assignments/DC_Cohort/Assignment1.md b/02_activities/assignments/DC_Cohort/Assignment1.md index d5ff55ce4..de4617d06 100644 --- a/02_activities/assignments/DC_Cohort/Assignment1.md +++ b/02_activities/assignments/DC_Cohort/Assignment1.md @@ -108,6 +108,8 @@ Please do not pick the exact same tables that I have already diagrammed. For exa *** +- + ## Section 2: You can start this section following *session 2*. @@ -209,5 +211,13 @@ Consider, for example, concepts of fariness, inequality, social structures, marg ``` -Your thoughts... +When I graduated from primary school and needed to get into middle school,  the scheme was based on a scoring system. This point system has various strict criteria. For example, it calculates whether your family has lived in the district and paid taxes for a long enough period, as well as whether you have the official city identity registration. The current school admission system is built around a specific type of family: the traditional, locally born family with only one child. The policy is unfair for the families who have moved to the city and worked hard here. This design shows how system design can isolate certain groups and take away their right to a good education. + +  + +  + +Another important factor in this system is if you have one birth in your family, then you get 50 more points. But the problem happens to families where they have twins in one birth. But the database only links to one child, not exactly one birth. Thus, a family with twins cannot get these very important 50 points in this important recruitment process. The design of the database and definition of the policy are not fair enough. Because twins are registered as two distinct children, the system automatically disqualifies them from the single-birth category. However, the rule was intended to award the family a single birth to control the fast-paced growth of the population. But the database system counts how many children are in a family instead of the number of births. It unfairly hurts families with twins like my family. Even though I have another score from other criteria to let me get into the school. I knew some family they really need this 50 score to help their child to get a good education chance. To fix this, the database system needs to be updated. It should record a single birth instead of the number of children. + + ``` diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 72f8c71fd..48e53be61 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -48,8 +48,12 @@ 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. +![image](images/ass2-pic1.png) + + #### Prompt 2 We want to create employee shifts, splitting up the day into morning and evening. Add this to the ERD. +![image](images/ass2-pic2.png) #### 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 +61,31 @@ 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... +Overwrite is Type 1 SCD: +CUSTOMER_ADDRESS (address_id PK, +customer_id FK UNIQUE, +street_address, city, +province, +postal_code, +country, +last_updated) + + + +Retain chainges is Type 2 SCD : +CUSTOMER_ADDRESS (address_id PK, +customer_id FK, +street_address, +city, +province, +postal_code, +country, +effective_start_date, +effective_end_date, +is_current) + + +The overwrite design is Type 1; the design that retains changes is Type 2. ``` *** @@ -193,5 +221,9 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c ``` -Your thoughts... +This article reminds me of a recent news story I saw several days ago. There are many companies that hired huge numbers of people to make industrial components. And they wear camera glasses on their heads. This machine has two functions. First, it records the workers' behaviors to train the robots later. Second, they use the camera as an invigilator. This makes sure the workers don't take any rest in their work. + +Here are the problems it raises. First, after the data is collected, the company that collected the data earns money from this data. Other companies that use this data to train models may improve their robots and later earn money by selling the trained robots to factories. But the workers who provide data don't get any pay in any part of the pipeline of this data usage. But it doesn't mean they are lesser contributors in the whole business process. Does the business give the workers enough credit? Just like the Mechanical Turk workers in the article, these workers do not get enough credit for their work, and they need something like copyright so that they can earn revenue whenever their data is used. Also, in the article the ImageNet workers only get coins, but the team who uses the data gets the most credit and wins the prize. Is it a fair thing to the data labeling job compared to the modeling job? + +Besides, the camera is used to make sure they do not take a rest. It's like their privacy is stolen and you cannot complain because of the burden to survive and earn a salary. ``` diff --git a/02_activities/assignments/DC_Cohort/assignment1.sql b/02_activities/assignments/DC_Cohort/assignment1.sql index a80923591..5f1bf0bf7 100644 --- a/02_activities/assignments/DC_Cohort/assignment1.sql +++ b/02_activities/assignments/DC_Cohort/assignment1.sql @@ -7,7 +7,7 @@ /* 1. Write a query that returns everything in the customer table. */ --QUERY 1 - +SELECT * FROM customer; --END QUERY @@ -17,7 +17,9 @@ sorted by customer_last_name, then customer_first_ name. */ --QUERY 2 - +SELECT * FROM customer +ORDER BY customer_last_name, customer_first_name +LIMIT 10; --END QUERY @@ -28,7 +30,9 @@ sorted by customer_last_name, then customer_first_ name. */ Limit to 25 rows of output. */ --QUERY 3 - +SELECT * FROM customer_purchases +WHERE product_id IN (4,9) +LIMIT 25; --END QUERY @@ -42,7 +46,10 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: Limit to 25 rows of output. */ --QUERY 4 - +SELECT *, quantity * cost_per_quantity AS price +FROM customer_purchases +WHERE customer_id BETWEEN 8 AND 10 +LIMIT 25; @@ -56,7 +63,11 @@ columns and add a column called prod_qty_type_condensed that displays the word if the product_qty_type is “unit,” and otherwise displays the word “bulk.” */ --QUERY 5 - +SELECT product_id, product_name, + CASE WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed +FROM product; --END QUERY @@ -66,7 +77,14 @@ if the product_qty_type is “unit,” and otherwise displays the word “bulk. add a column to the previous query called pepper_flag that outputs a 1 if the product_name contains the word “pepper” (regardless of capitalization), and otherwise outputs 0. */ --QUERY 6 - +SELECT product_id, product_name, + CASE WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed, + CASE WHEN LOWER(product_name) LIKE '%pepper%' THEN 1 + ELSE 0 + END AS pepper_flag +FROM product; @@ -79,8 +97,13 @@ vendor_id field they both have in common, and sorts the result by market_date, t Limit to 24 rows of output. */ --QUERY 7 - - +SELECT * +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba + ON v.vendor_id = vba.vendor_id +ORDER BY vba.market_date, v.vendor_name +LIMIT 24; +-- --END QUERY @@ -93,7 +116,9 @@ Limit to 24 rows of output. */ at the farmer’s market by counting the vendor booth assignments per vendor_id. */ --QUERY 8 - +SELECT vendor_id, COUNT(*) AS booth_rental_count +FROM vendor_booth_assignments +GROUP BY vendor_id; --END QUERY @@ -106,7 +131,14 @@ of customers for them to give stickers to, sorted by last name, then first name. HINT: This query requires you to join two tables, use an aggregate function, and use the HAVING keyword. */ --QUERY 9 - +SELECT c.customer_id, c.customer_first_name, c.customer_last_name, + SUM(cp.quantity * cp.cost_per_quantity) AS total_spent +FROM customer AS c +INNER JOIN customer_purchases AS cp + ON c.customer_id = cp.customer_id +GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name +HAVING SUM(cp.quantity * cp.cost_per_quantity) > 2000 +ORDER BY c.customer_last_name, c.customer_first_name; --END QUERY @@ -125,6 +157,15 @@ VALUES(col1,col2,col3,col4,col5) */ --QUERY 10 +DROP TABLE IF EXISTS temp.new_vendor; + +CREATE TABLE temp.new_vendor AS +SELECT * FROM vendor; + +INSERT INTO temp.new_vendor +VALUES (10, 'Thomass Superfood Store', 'Fresh Focused', 'Thomas', 'Rosenthal'); + +SELECT * FROM temp.new_vendor; @@ -138,6 +179,11 @@ HINT: you might need to search for strfrtime modifers sqlite on the web to know and year are! Limit to 25 rows of output. */ --QUERY 11 +SELECT customer_id, + strftime('%m', market_date) AS month, + strftime('%Y', market_date) AS year +FROM customer_purchases +LIMIT 25; @@ -153,7 +199,12 @@ but remember, STRFTIME returns a STRING for your WHERE statement... AND be sure you remove the LIMIT from the previous query before aggregating!! */ --QUERY 12 - +SELECT customer_id, + SUM(quantity * cost_per_quantity) AS total_spent +FROM customer_purchases +WHERE strftime('%m', market_date) = '04' + AND strftime('%Y', market_date) = '2022' +GROUP BY customer_id; --END QUERY diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index 4079c18ae..3a7822b9f 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -24,7 +24,9 @@ All the other rows will remain the same. */ --QUERY 1 - +SELECT +product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_list +FROM product; --END QUERY @@ -41,7 +43,11 @@ 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, product_id, vendor_id, quantity, transaction_time, + 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 @@ -52,7 +58,13 @@ 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 customer_id, market_date, product_id, vendor_id, quantity, transaction_time, + DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number + FROM customer_purchases +) AS x +WHERE x.visit_number = 1; @@ -65,7 +77,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 customer_id, product_id, market_date, vendor_id, quantity, transaction_time, + 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; @@ -85,7 +101,12 @@ 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 @@ -93,7 +114,13 @@ 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 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]'; @@ -111,7 +138,22 @@ 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 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 best_rank, + RANK() OVER (ORDER BY total_sales ASC) AS worst_rank + FROM daily_sales +) +SELECT market_date, total_sales, 'best day' AS day_type +FROM ranked_sales WHERE best_rank = 1 +UNION +SELECT market_date, total_sales, 'worst day' AS day_type +FROM ranked_sales WHERE worst_rank = 1; --END QUERY diff --git a/02_activities/assignments/DC_Cohort/images/ass2-pic1.png b/02_activities/assignments/DC_Cohort/images/ass2-pic1.png new file mode 100644 index 000000000..4fb88113e Binary files /dev/null and b/02_activities/assignments/DC_Cohort/images/ass2-pic1.png differ diff --git a/02_activities/assignments/DC_Cohort/images/ass2-pic2.png b/02_activities/assignments/DC_Cohort/images/ass2-pic2.png new file mode 100644 index 000000000..830b9e0b7 Binary files /dev/null and b/02_activities/assignments/DC_Cohort/images/ass2-pic2.png differ diff --git a/02_activities/assignments/DC_Cohort/images/section1D.png b/02_activities/assignments/DC_Cohort/images/section1D.png new file mode 100644 index 000000000..22a1d2104 Binary files /dev/null and b/02_activities/assignments/DC_Cohort/images/section1D.png differ