Skip to content
Open
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
4 changes: 2 additions & 2 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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...
This is about how a database handle data that don't change very frequently, i.e., slowly changing. Type 1 can refer to an Overwrite architecture, where all past information will no longer be stored at any place in the database when it is replaced by new information. This type help save the storage space of the database, and offer a very simple schema or queries. Type 2 can refer to an Full history architecture, where all past information will be retained in a different row or column when new information is encoded. This type keeps track of all information, making the database more comprehensive for future track or research.
```

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


```
Your thoughts...
The author seems to argue that artifical neural network are mainly driven by human work. This leads to two points: 1. these models embed biases and are not objective or automated and, 2. human efforts are actually behind the fast progression of AI model. To me, both points are undoubtfully true and very intuitive. First, I think everyone who studies AI will admit that artifical neural networks are not objective. These models strongly depend on what data are feeded to them, how the output are set, or even how many layers/nodes are implemented into the models. In other words, each step to build a network are human decisions, and they thus are extremely vulnerable to human biases. In fact, the issue of getting output simply because what you put in has been a major critisim for cognitive computational models. When one constructs a model following a specific mechanism to perform a task, its successful results in this task is a tautology rather than demonstration of science. The model simply reflects the builders' assumptions, while no cognition per se are examined or proved. Besides, artifical network and automation are two distinct, or even opposite concepts. AI studies mainly aim to develop a powerful statistical learning system that rely on human defined data, objectives, and engineering, making it inherently fragile but adaptive. These properties are, however, not acceptable to an automated system, which is supposed to be free of human intervention, stable, and follow clear rules. Therefore, they are two completely different field of research, and should not be treated as the same thing. Second, given the nature that AI models rely on human intervention and construction, it's apparent that human efforts are essential for all works in AI development. This also goes back to the distinction between automation and AI. When people truly think AI equals automation, many people who took effort on it, including data labors, data scientists, annotators, etc., are all undervalued. In other words, AI research is not just engineering, yet it should be treated as comparable to human-relevant research like cognitive, behavioral, or social works and thus should share similar ethic protocols and acknowledgements as them.
```
160 changes: 148 additions & 12 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ 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.) */

/* Here is the code that only removes the NULL in columns*/
SELECT
COALESCE(product_size, '') AS product_size,
COALESCE(product_qty_type, 'unit') AS product_qty_type
FROM product;



/* I was not sure if you want us to direct make a list without NULL entry, so I made the list in addition to the column NULL removal*/
SELECT
product_name || ', ' || COALESCE(product_size, '')|| ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product;



--Windowed Functions
Expand All @@ -32,18 +45,42 @@ each new market date for each customer, or select only the unique market dates p
(without purchase details) and number those visits.
HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */


SELECT
customer_id,
market_date,
ROW_NUMBER ( ) OVER (
PARTITION BY customer_id
ORDER BY market_date
) AS visit_number
FROM customer_purchases;

/* 2. Reverse the numbering of the query from a part so each customer’s most recent visit is labeled 1,
then write another query that uses this one as a subquery (or temp table) and filters the results to
only the customer’s most recent visit. */

SELECT *
FROM (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS Recency_based_ranking
FROM customer_purchases
) t
WHERE Recency_based_ranking = 1;


/* 3. Using a COUNT() window function, include a value along with each row of the
customer_purchases table that indicates how many different times that customer has purchased that product_id. */


SELECT
customer_id,
product_id,
COUNT(*) AS purchase_frequency
FROM customer_purchases
GROUP BY customer_id, product_id;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -57,22 +94,60 @@ 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. */

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;


/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */


SELECT *
FROM product
WHERE product_size REGEXP '[0-9]' ;

-- UNION
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.*/
/* Then hint descriptions somehow give me errors, so I deleted them.*/

/* create a temp table for the total sale values for each day*/
DROP TABLE IF EXISTS sale_value_summary;

CREATE TEMPORARY TABLE sale_value_summary AS
SELECT market_date,
SUM(COALESCE(quantity,0) * COALESCE(cost_to_customer_per_qty, 0)) as total_scales_value_per_day
FROM customer_purchases
GROUP BY market_date;

/* find the highest sale day*/
DROP TABLE IF EXISTS top_sale;

HINT: There are a possibly a few ways to do this query, but if you're struggling, try the following:
1) Create a CTE/Temp Table to find sales values grouped dates;
2) Create another CTE/Temp table with a rank windowed function on the previous query to create
"best day" and "worst day";
3) Query the second temp table twice, once for the best day, once for the worst day,
with a UNION binding them. */
CREATE TEMPORARY TABLE top_sale AS
SELECT *
FROM sale_value_summary
ORDER BY total_scales_value_per_day DESC
LIMIT 1;

/* find the lowest sale day*/
DROP TABLE IF EXISTS worst_sale;

CREATE TEMPORARY TABLE worst_sale AS
SELECT *
FROM sale_value_summary
ORDER BY total_scales_value_per_day
LIMIT 1;

/* combine top and worst sale days with UNION function */
SELECT *
FROM top_sale
UNION
SELECT *
FROM worst_sale;



Expand All @@ -89,6 +164,21 @@ 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). */

WITH c AS (
SELECT COUNT(*) AS n_customers
FROM customer
)
SELECT
v.vendor_name,
vi.vendor_id,
p.product_name,
vi.product_id,
5 * vi.original_price * c.n_customers AS total_earnings
FROM vendor_inventory vi
JOIN vendor v ON v.vendor_id = vi.vendor_id
JOIN product p ON p.product_id = vi.product_id
CROSS JOIN c
GROUP BY v.vendor_name, p.product_name;


-- INSERT
Expand All @@ -97,19 +187,35 @@ 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`. */

drop table if exists product_units;

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

/*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). */


INSERT INTO product_units (product_id, product_name, product_size, product_category_id, product_qty_type, snapshot_timestamp)
VALUES (7, 'Apple Pie', 'small', '3', 'unit', CURRENT_TIMESTAMP);

-- DELETE
/* 1. Delete the older record for the whatever product you added.

HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/


CREATE TEMP TABLE newest_product AS
SELECT product_id, snapshot_timestamp
FROM product_units
ORDER BY snapshot_timestamp DESC
LIMIT 1;

DELETE FROM product_units
WHERE product_id = (SELECT product_id FROM newest_product)
AND snapshot_timestamp < (SELECT snapshot_timestamp FROM newest_product);

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -129,5 +235,35 @@ 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. */


ALTER TABLE product_units
ADD current_quantity INT;

/* find the last quantity for each product */
drop table if EXISTS newest_quant;
CREATE TEMPORARY TABLE newest_quant AS
SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY product_id
ORDER BY market_date DESC
) AS order_number
FROM vendor_inventory
) ranked
WHERE order_number = 1;

/* replace all NULL with 0 value*/
UPDATE product_units AS pu
SET current_quantity = COALESCE(nq.quantity, 0)
FROM newest_quant AS nq
WHERE pu.product_id = nq.product_id;

UPDATE product_units AS pu
SET current_quantity = 0
WHERE NOT EXISTS (SELECT 1 FROM newest_quant nq WHERE nq.product_id = pu.product_id);





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
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.
Binary file modified 05_src/sql/farmersmarket.db
Binary file not shown.