diff --git a/02_activities/assignments/DC_Cohort/Assignment1.md b/02_activities/assignments/DC_Cohort/Assignment1.md index f650c9752..125e5af6e 100644 --- a/02_activities/assignments/DC_Cohort/Assignment1.md +++ b/02_activities/assignments/DC_Cohort/Assignment1.md @@ -209,5 +209,11 @@ Consider, for example, concepts of fariness, inequality, social structures, marg ``` -Your thoughts... +Databases are part of many areas of our everyday lives in today’s digital world, including university portals, banking apps, navigation systems, search engines, and streaming platforms. The way these systems are designed - their underlying structure or “schema” - shapes how people are categorized and understood within them. For example, many databases require users to select from fixed options such as marital status or gender, reflecting assumptions about what categories are allowed and used to link datapoints within the data base. + +Another important value reflected in many modern data systems is a users consumption behavior. In particular, databases are often built to track, predict, and encourage user spending and engagement. For instance, online shopping platforms frequently require users to create an account rather than allowing guest checkout, enabling the system to store personal data and generate “recommended for you” suggestions. + +A further value embedded in many databases is locality or residency. Many systems, such as those used for credit cards or Social Insurance Numbers, require a fixed address or proof of residence. This reflects an assumption that individuals are tied to a specific place. However, in an increasingly global and mobile world, where remote work and travel are more common, this emphasis on having a permanent address may not reflect everyone’s reality. + +The underlying trend/challenge in values that are embedded in databases are the increasingly importance of personalization. Adds, medicine, consumer goods are being tailored more and more effectively to every individual. This, on the one hand necessitates databases to build a personalized user profile, however, in the future this will also raise the need for personalized answer options, making databases (which ultimately categorize and structure social networks/data) more fluid and dynamic. ``` diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 01f991d02..07b93057a 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -56,7 +56,7 @@ 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... +Slowly changing dimensions are tables whos entries may change, for example the customer adress. If only the most up to date version is important, and changes don't have to be retained, we call these Type 1 slowly changing dimensions (SCD), in which the current value overwrites the old one. This may be the case, if the customer adress has a typo that we want to overwrite. If changes have to be retained however - for example if a large customer changes their adress, but we are interested in keeping their adress history - a Type 2 SCD architecture should be used (for example temporal tables in SQL). ``` *** @@ -191,5 +191,5 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c ``` -Your thoughts... +The article highlights how AI training is not only dependent on the AI architecture itself but strongly influenced by the training dataset, which is created and curated by humans. As such, human ethical values shape the artificial intelligence’s capability of making judgment calls. While it may seem as if AI makes objective, rational decisions, it can’t create knowledge or assessment guidelines on its own, but is trained on such data. Every evaluation by AI is based on an underlying dataset, which is influenced by human morals. Even if it may seem like a very objective task, like classifying images, the training dataset that AI learns from can be biased or simply misrepresented. While a bias might, to the human eye, not be noticeable (especially in very large datasets), machine learning algorithms can pick such patterns up and amplify them during the learning process. This can create answers that were never anticipated by the dataset creators. This highlights the importance of moderating content and carefully filtering the data that technology later automates on. AI is not self-sufficient, but heavily depends on our initial judgment calls, repeating and learning from patterns that we introduce when communicating with it. Data preprocessing is a key objective in the advancement of AI and will have to integrate carefully tailored policies, ensuring an age of technology in which AI is not only task-optimized, but also adapted to the ethical standards that we as a human society value. ``` diff --git a/02_activities/assignments/DC_Cohort/Data_logicalmap.pdf b/02_activities/assignments/DC_Cohort/Data_logicalmap.pdf new file mode 100644 index 000000000..a123311eb Binary files /dev/null and b/02_activities/assignments/DC_Cohort/Data_logicalmap.pdf differ diff --git a/02_activities/assignments/DC_Cohort/ERD_BookStore_Assignment2.png b/02_activities/assignments/DC_Cohort/ERD_BookStore_Assignment2.png new file mode 100644 index 000000000..431e9028e Binary files /dev/null and b/02_activities/assignments/DC_Cohort/ERD_BookStore_Assignment2.png differ diff --git a/02_activities/assignments/DC_Cohort/assignment1.sql b/02_activities/assignments/DC_Cohort/assignment1.sql index 2ec561e2a..2af975f4e 100644 --- a/02_activities/assignments/DC_Cohort/assignment1.sql +++ b/02_activities/assignments/DC_Cohort/assignment1.sql @@ -6,7 +6,7 @@ --SELECT /* 1. Write a query that returns everything in the customer table. */ --QUERY 1 - +SELECT * FROM customer; @@ -16,9 +16,9 @@ /* 2. Write a query that displays all of the columns and 10 rows from the customer table, sorted by customer_last_name, then customer_first_ name. */ --QUERY 2 - - - +SELECT * FROM customer +ORDER BY customer_first_name +LIMIT 10; --END QUERY @@ -27,7 +27,9 @@ sorted by customer_last_name, then customer_first_ name. */ /* 1. Write a query that returns all customer purchases of product IDs 4 and 9. Limit to 25 rows of output. */ --QUERY 3 - +SELECT * FROM customer_purchases +WHERE product_id IN (4, 9) +LIMIT 25 @@ -42,7 +44,10 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: Limit to 25 rows of output. */ --QUERY 4 - + SELECT *, quantity * cost_to_customer_per_qty AS Price +FROM customer_purchases +WHERE customer_id BETWEEN 8 AND 10 +LIMIT 25; @@ -55,6 +60,13 @@ Using the product table, write a query that outputs the product_id and product_n columns and add a column called prod_qty_type_condensed that displays the word “unit” 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 @@ -66,7 +78,18 @@ 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,7 +102,10 @@ 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_booth_assignments +INNER JOIN vendor ON vendor_booth_assignments.vendor_id = vendor.vendor_id +ORDER BY market_date, vendor_name +LIMIT 24 --END QUERY @@ -92,8 +118,10 @@ Limit to 24 rows of output. */ /* 1. Write a query that determines how many times each vendor has rented a booth at the farmer’s market by counting the vendor booth assignments per vendor_id. */ --QUERY 8 - - +SELECT vendor_id, + COUNT(vendor_id) AS booth_count +FROM vendor_booth_assignments +GROUP BY vendor_id; --END QUERY @@ -106,6 +134,12 @@ 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 customer.customer_last_name, customer.customer_first_name, customer.customer_id, SUM(customer_purchases.cost_to_customer_per_qty) AS total_spent +FROM customer_purchases +JOIN customer ON customer_purchases.customer_id = customer.customer_id +GROUP BY customer.customer_id +HAVING SUM(cost_to_customer_per_qty) > 2000 +ORDER BY customer_last_name, customer_first_name; @@ -124,7 +158,10 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ --QUERY 10 - +CREATE TEMPORARY TABLE new_vendor AS +SELECT * FROM vendor +UNION ALL +SELECT '10', 'Thomas Superfood Store', 'Fresh Focused', 'Thomas', 'Rosenthal'; @@ -138,7 +175,9 @@ 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 @@ -152,6 +191,10 @@ HINTS: you will need to AGGREGATE, GROUP BY, and filter... 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_to_customer_per_qty) AS total_spend +FROM customer_purchases +WHERE market_date LIKE '2022-04%' +GROUP BY customer_id; diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index f7515f625..f19e8b2a9 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -22,9 +22,12 @@ 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 ProductListForManager +FROM product --END QUERY @@ -41,8 +44,13 @@ 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 ASC) AS VisitNumber +FROM customer_purchases +WHERE market_date < '2022-04-29'; --END QUERY @@ -52,9 +60,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 customer_id, market_date, VisitNumber +FROM (SELECT customer_id, market_date, +ROW_NUMBER () OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC) AS VisitNumber + FROM customer_purchases) + AS MostRecentVisit WHERE VisitNumber = 1; --END QUERY @@ -66,8 +78,13 @@ 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 DISTINCT +customer_id, product_id, +COUNT (*) OVER ( +PARTITION BY product_id , customer_id +ORDER BY product_id ASC) AS CustomerHasBoughtProductNTimes +FROM customer_purchases +WHERE market_date < '2022-04-29'; --END QUERY @@ -84,18 +101,21 @@ 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 *, +TRIM(NULLIF(SUBSTR(product_name, INSTR(product_name, '-') + 1), product_name)) 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 * +FROM( +SELECT *, +TRIM(NULLIF(SUBSTR(product_name, INSTR(product_name, '-') + 1), product_name)) AS Description +FROM product) +WHERE product_size REGEXP '[0-9]'; --END QUERY @@ -110,9 +130,19 @@ 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 - - - +SELECT market_date, Min(TotalSalePerDay) AS TotalSale, 'MinSaleDay' AS MinMaxSaleDay +FROM( +SELECT quantity, cost_to_customer_per_qty, market_date, +SUM( quantity*cost_to_customer_per_qty) AS TotalSalePerDay +FROM customer_purchases +GROUP BY market_date) AS LowestSaleDate +UNION +SELECT market_date, Max(TotalSalePerDay) AS TotalSale, 'MaxSaleDay' AS MinMaxSaleDay +FROM( +SELECT quantity, cost_to_customer_per_qty, market_date, +SUM( quantity*cost_to_customer_per_qty) AS TotalSalePerDay +FROM customer_purchases +GROUP BY market_date) --END QUERY @@ -131,9 +161,13 @@ 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 - - - +SELECT vendor.vendor_name, product.product_name, temptable.FiveProductsSoledPrice +FROM( +SELECT vendor_id, product_id, 5*(product_id*original_price) AS FiveProductsSoledPrice +FROM vendor_inventory +GROUP BY product_id) AS temptable +JOIN vendor ON temptable.vendor_id = vendor .vendor_id +JOIN product ON temptable.product_id = product.product_id; --END QUERY @@ -144,9 +178,11 @@ 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 @@ -154,9 +190,8 @@ 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 +VALUES ('30', 'ApplePie', 'small', '3', 'unit', CURRENT_TIMESTAMP); --END QUERY @@ -166,9 +201,8 @@ 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 MAX(snapshot_timestamp) FROM product_units ); --END QUERY @@ -191,7 +225,21 @@ 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 = ( + SELECT COALESCE(quantity, 0) + FROM vendor_inventory + WHERE vendor_inventory.product_id = product_units.product_id + AND market_date = ( + SELECT MAX(market_date) + FROM vendor_inventory AS temptable + WHERE temptable.product_id = product_units.product_id + ) +) +WHERE product_units.product_id IN (SELECT product_id FROM vendor_inventory); --END QUERY diff --git a/05_src/sql/farmersmarket.db b/05_src/sql/farmersmarket.db index 4720f2483..610544603 100644 Binary files a/05_src/sql/farmersmarket.db and b/05_src/sql/farmersmarket.db differ