Skip to content
Merged
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
20 changes: 18 additions & 2 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,23 @@ 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 old data with new data
Customer_Address_Type-1
customer_id
customer_address
customer_province
customer_postal_code
customer_updated_at

Type 2 = preserves historical changes by creating a new record for each change
Customer_Address_Type-1
customer_id
customer_address
customer_province
customer_postal_code
effective_date
end_date
is_current
```

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


```
Your thoughts...
The article was very illuminating and provided me more context and insight to how neural nets are developed. We are not often sharing or talking about this side of AI, particularly in my field of health systems research. It feels like it’s branded to us to only know the ‘user interface’ side and not about the backend/internal structures, especially because it’s labelled as the ‘next best thing for healthcare’. I perceive this as a major ethical issue where there is a lack of transparency on AI development in my field, in media, and general rhetoric of it all. It has become a ‘black box’. I will admit, when I first heard about AI years ago, I viewed it as a positive impact to the world and jumped on the bandwagon of “this will fix the healthcare system”. But it was only really when I had conversations with different people where I started to have a more critical lens on AI. I discuss with my husband often (who’s background is in theoretical particle physics and now computational neuroscience), where he shared more about what AI actually is and the research behind it (just like how the article frames it actually). And it was also when I had brief conversations with my older sister (who’s background is in political science and history, with an affinity to learning about environmental impacts), where she shared the environmental and labor concerns related to AI. And finally in my part-time research work, I get the opportunity to observe roundtables where some senior decision-makers in health (think ministers of health and CEOs of hospitals), and they talk about how our infrastructure (e.g., workforce delivery, literal data infrastructures because we still use fax in Canada) needs to be further developed before we even start to think about how we embed AI into the workflows of healthcare. This is where I started to have a more critical perspective on AI, and what it can actually do, the potential consequences, and what it actually means for health systems research. This brings up another issue highlighted in the article where there have been ethical concerns with the training sets, including the labour force and the way images are labelled. For the latter, that’s a major concern because it will have downstream effects to how AI is used in healthcare. Healthcare is sadly already marred with discrimination and systemic racism, and with training sets that are biased, there could have dire consequences to the quality of patient care. Not only is it about patient demographic characteristics but imagine a clinical decision support system that is developed but it’s been trained to ignore certain symptoms or fail flag to doctors about certain conditions related to a patient (e.g., misinterpreting symptoms among marginalized populations, especially women). Positively, there are groups who are aware of the ethical concerns around AI use in healthcare, yet there needs to be better oversight and transparency of how the back-end of AI has been developed. Overall, the article brings up valid concerns, and I wish we had more discourse publicly and openly about how these systems continue to perpetuate very human challenges (because it is built by us after all!), especially because of the way the population continues to embed AI into their everyday lives.
```
170 changes: 133 additions & 37 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ 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


Expand All @@ -40,9 +40,15 @@ 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,
dense_rank () OVER (
PARTITION BY customer_id
ORDER BY market_date
) AS visit_number
FROM customer_purchases
WHERE market_date <'2022-04-29';

--END QUERY

Expand All @@ -52,9 +58,20 @@ 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
FROM (
SELECT DISTINCT
customer_id,
market_date,
DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number_reverse
FROM customer_purchases
) AS ranked_visits
WHERE visit_number_reverse = 1;

--END QUERY

Expand All @@ -65,9 +82,16 @@ 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,
COUNT(product_id) OVER (
PARTITION BY customer_id, product_id
ORDER BY market_date
) AS product_purchase_count
FROM customer_purchases
WHERE market_date < '2022-04-29';

--END QUERY

Expand All @@ -84,18 +108,25 @@ 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
FROM product
WHERE product_size REGEXP '[0-9]';

--END QUERY

Expand All @@ -110,9 +141,36 @@ 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_by_date AS (
SELECT
market_date,
SUM(quantity*cost_to_customer_per_qty) 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 top_rank,
RANK() OVER (ORDER BY total_sales ASC) AS bottom_rank
FROM sales_by_date
)
SELECT
market_date,
total_sales,
'Highest Sales' AS sales_category
FROM ranked_sales
WHERE top_rank = 1

UNION


SELECT
market_date,
total_sales,
'Lowest Sales' AS sales_category
FROM ranked_sales
WHERE bottom_rank = 1;

--END QUERY

Expand All @@ -131,9 +189,28 @@ 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
v.vendor_name,
p.product_name,
5*COUNT (c.customer_id)*vi.original_price AS potential_revenue
FROM (
SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
) vi
JOIN vendor v
ON vi.vendor_id = v.vendor_id
JOIN product p
ON vi.product_id = p.product_id
CROSS JOIN (
SELECT customer_id
FROM customer
) c
GROUP BY
v.vendor_name,
p.product_name;

--END QUERY

Expand All @@ -144,20 +221,33 @@ 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 (
product_id,
product_name,
product_size,
product_qty_type,
snapshot_timestamp
)
VALUES (
999,
'Apple Pie',
'1 pie',
'unit',
CURRENT_TIMESTAMP
);
--END QUERY


Expand All @@ -166,8 +256,9 @@ 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 = 'Apple Pie'
AND product_id <> 999;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your query correctly filters by product_id and product_name . The requirement is to delete the older record for the whatever product you added. You can find the oldest record (e.g., using MIN(snapshot_timestamp)) and delete only that row.


--END QUERY
Expand All @@ -190,11 +281,16 @@ 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 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



Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading