Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ 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:
CUSTOMER_ADDRESS has customer_id, street_address, address_city, address_country, and address_postal_code columns. This architecture overwrites the address columns when a customer updates their address.

Type 2:
CUSTOMER_ADDRESS has a unique_key, start_date, end_date, and is_current coolumns plus all the columns from the type 1 architecture. This retains old records and identifies the customer's current address.
```

***
Expand Down Expand Up @@ -193,5 +197,19 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c


```
Your thoughts...
One tentent of this article is that machine learning models have innate bias due to the nature of human intervention
in creating training datasets. Specifically, the way in which entities are labeled is based on a human's understanding
of that entity. Our understanding of the world is infulenced by our experiences, beliefs, and environments. As such,
our understanding of the world varies between individuals. When a subset of individuals are tasked with creating a
training set, their biases are disproportionately represented in the data. As we integrate the products of machine
learning into our lives, and to help make decisions, those biases can be carried through to influence the world we
share.

Another concept discussed in the article is different forms of labour execution. First, the article discussed the use of machines to perform tasks carried out by humans. This raises concerns about labour, and how much we should utilize
technology to perform labour. How do we support those whose jobs might be replaced by technology? Which tasks should we
allow technology to execute without human oversight? Does replacing human labour with technology have a net benefit or
not? Second, the article discusses the use of Mechanical Turk, which is a crowdsourcing marketplace. Some argue that
the use of such marketplaces is a way to get around labour laws about fair wages. Are crowdsourced marketplaces that pay
small monetary amounts a form of exploitation, or does it provide widespread economic oportunities that distributes
labour globally? Perhaps, as with many complex topics, the answer is a third version that acknowledges both of the former.
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 116 additions & 27 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ 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

Expand All @@ -41,9 +42,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 *,
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


Expand All @@ -53,8 +56,15 @@ 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,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number
FROM customer_purchases
) x
WHERE x.visit_number =1;

--END QUERY

Expand All @@ -66,8 +76,10 @@ 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

Expand All @@ -85,17 +97,28 @@ 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,
NULLIF(
TRIM(
SUBSTR(product_name, INSTR(product_name, '-')+1)),
TRIM(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
product_name,
product_size,
NULLIF(
TRIM(
SUBSTR(product_name, INSTR(product_name, '-')+1)),
TRIM(product_name)) AS description
FROM product
WHERE product_size REGEXP '\d';

--END QUERY

Expand All @@ -111,8 +134,29 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */
--QUERY 7



SELECT * FROM
(
SELECT
market_date,
SUM(quantity * cost_per_quantity) AS date_total_sales
FROM customer_purchases
GROUP BY market_date
ORDER BY date_total_sales DESC
LIMIT 1
)

UNION

SELECT * FROM
(
SELECT
market_date,
SUM(quantity * cost_per_quantity) AS date_total_sales
FROM customer_purchases
GROUP BY market_date
ORDER BY date_total_sales ASC
LIMIT 1
);

--END QUERY

Expand All @@ -132,8 +176,34 @@ 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_product.vendor_name,
vendor_product.product_name,
SUM(5 * vendor_product.original_price) AS total_revenue
FROM
(
SELECT DISTINCT
v.vendor_name,
p.product_name,
vi.original_price
FROM vendor_inventory AS vi
JOIN vendor AS v
ON vi.vendor_id = v.vendor_id
JOIN product AS p
ON vi.product_id = p.product_id
) AS vendor_product
CROSS JOIN
(
SELECT DISTINCT
customer_id
FROM customer
)
GROUP BY
vendor_product.vendor_name,
vendor_product.product_name
ORDER BY
vendor_product.vendor_name,
vendor_product.product_name;

--END QUERY

Expand All @@ -145,8 +215,13 @@ 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



DROP TABLE IF EXISTS product_units;
CREATE TABLE product_units AS
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

--END QUERY

Expand All @@ -155,8 +230,8 @@ 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
VALUES (5, 'Whole Wheat Bread', '1.5 lbs', 3, 'unit', CURRENT_TIMESTAMP);

--END QUERY

Expand All @@ -167,8 +242,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 product_name = 'Whole Wheat Bread'
AND snapshot_timestamp < (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Whole Wheat Bread'
);

--END QUERY

Expand All @@ -191,10 +271,19 @@ 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 vi.quantity
FROM vendor_inventory AS vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1
),
0
);

--END QUERY



Loading