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
Binary file not shown.
Binary file not shown.
182 changes: 179 additions & 3 deletions 02_activities/assignments/Cohort_8/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ 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. */

DROP VIEW IF EXISTS list_for_products;
CREATE VIEW IF NOT EXISTS list_for_products AS

SELECT
coalesce (product_name, '') || ', ' || coalesce (product_size, '') || ' (' || coalesce (product_qty_type, 'unit') || ')' AS list_of_products
FROM product;

SELECT * from list_for_products ;


--Windowed Functions
Expand All @@ -34,18 +41,46 @@ 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,
transaction_time,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date, transaction_time) AS No_of_visit
FROM customer_purchases
ORDER BY customer_id, market_date, transaction_time;


/* 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
customer_id,
market_date,
transaction_time
FROM (
SELECT
customer_id,
market_date,
transaction_time,
DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS No_of_visit
FROM customer_purchases
) AS ranked_visits
WHERE No_of_visit= 1
ORDER BY customer_id;


/* 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,
market_date,
transaction_time,
COUNT(*) OVER (PARTITION BY customer_id, product_id) AS times_purchased_this_product
FROM customer_purchases
ORDER BY customer_id, product_id, market_date;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -59,10 +94,32 @@ 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 value_of_product
FROM product;


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

SELECT
product_id,
product_name,
product_size,
CASE
WHEN INSTR(product_name, '-') > 0 THEN
TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
ELSE
NULL
END AS value_of_product
FROM product
WHERE product_size REGEXP '[0-9]'
ORDER BY product_name;


-- UNION
Expand All @@ -75,6 +132,43 @@ 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. */

-- Step 1:
WITH daily_sales AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date
),

-- Step 2:
ranked_sales AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS rank_best,
RANK() OVER (ORDER BY total_sales ASC) AS rank_worst
FROM daily_sales
)

-- Step 3:
SELECT
market_date,
total_sales,
'Highest Sales' AS sales_type
FROM ranked_sales
WHERE rank_best = 1

UNION

SELECT
market_date,
total_sales,
'Lowest Sales' AS sales_type
FROM ranked_sales
WHERE rank_worst = 1

ORDER BY total_sales DESC;



Expand All @@ -92,19 +186,82 @@ How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */


SELECT
v.vendor_name,
p.product_name,
COUNT(DISTINCT c.customer_id) AS number_of_customers,
COUNT(DISTINCT c.customer_id) * 5 AS total_units_sold,
COUNT(DISTINCT c.customer_id) * 5 * vi.original_price AS total_revenue_per_product
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 customer c
GROUP BY v.vendor_id, v.vendor_name, p.product_id, p.product_name, vi.original_price
ORDER BY v.vendor_name, p.product_name;

-- INSERT
/*1. Create a new table "product_units".
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 (
product_id INT(11) NOT NULL,
product_name VARCHAR(45) DEFAULT NULL,
product_size VARCHAR(45) DEFAULT NULL,
product_category_id INT(11) NOT NULL,
product_qty_type VARCHAR(45) DEFAULT NULL,
snapshot_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (product_id, snapshot_timestamp)
);

INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
SELECT
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

SELECT * FROM product_units;

/*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 (
999, -- product_id (use a unique ID)
'Apple Pie', -- product_name
'Large', -- product_size
1, -- product_category_id (adjust to valid category)
'unit', -- product_qty_type
CURRENT_TIMESTAMP -- snapshot_timestamp (current time)
);
SELECT * FROM product_units;

-- DELETE
/* 1. Delete the older record for the whatever product you added.
Expand All @@ -113,6 +270,7 @@ HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/




-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
First, add a new column, current_quantity to the table using the following syntax.
Expand All @@ -130,6 +288,24 @@ 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. */

ALTER TABLE product_units
ADD current_quantity INT;



-- Step 2: Update with last quantity per product
UPDATE product_units
SET current_quantity = (
SELECT COALESCE(SUM(vi.quantity), 0)
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
AND vi.market_date = (
SELECT MAX(vi2.market_date)
FROM vendor_inventory vi2
WHERE vi2.product_id = product_units.product_id
)
)
WHERE product_units.product_id IN (
SELECT DISTINCT product_id
FROM vendor_inventory
);
SELECT * FROM product_units;
Binary file modified 05_src/sql/farmersmarket.db
Binary file not shown.