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
97 changes: 85 additions & 12 deletions 02_activities/assignments/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ 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.) */

SELECT *
,ifnull (product_name, 'unknown')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

product_name replacement is not required by the question. But, you may need to handle product_qty_type

,coalesce (product_size, 'unit')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please replace product_size with ' ' (blank) instead of 'unit' as required


FROM product;

--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -32,21 +36,31 @@ 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 DISTINCT customer_id, market_date, DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date ASC
) AS visit
FROM customer_purchases;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please also answer the question using ROW_NUMBER()

/* 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 DISTINCT customer_id, market_date, DENSE_RANK() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit
FROM customer_purchases;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please filter the result to the most recent visit. Consider using WHERE

/* 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 *,
COUNT (*) OVER (PARTITION BY customer_id, product_id) as times_bought
FROM customer_purchases;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
These are separated from the product name with a hyphen.
Create a column using SUBSTR (and a couple of other commands) that captures these, but is otherwise NULL.
Remove any trailing or leading whitespaces. Don't just use a case statement for each product!
Expand All @@ -61,6 +75,15 @@ Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR w

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

SELECT product_name,
CASE
WHEN INSTR (product_name, '-') THEN
SUBSTR (product_name, RTRIM(LTRIM(INSTR(product_name, '-') + 1)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

INSTR() returns position, not a string. Consider LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))

ELSE NULL
END
AS discription
FROM product;



-- UNION
Expand All @@ -73,8 +96,20 @@ 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. */



WITH sales_table AS (
SELECT market_date, SUM(quantity * cost_to_customer_per_qty) AS sales
FROM customer_purchases
GROUP BY market_date
),
days_ranked_table AS (
SELECT market_date, sales,
RANK () OVER (ORDER BY sales ASC) as rank_asc,
RANK () OVER (ORDER BY sales DESC) as rank_desc
FROM sales_table
)
SELECT market_date, sales FROM days_ranked_table WHERE rank_desc == 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

syntax issue, should be single =

UNION ALL
SELECT market_date, sales FROM days_ranked_table WHERE rank_asc == 1;

/* SECTION 3 */

Expand All @@ -89,28 +124,40 @@ 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). */

/*
sorry this qeustion was too ambigous for me :( I dont know how to find out:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

First, count how many customers there are (y). Then, think about how many vendor-product combinations exist (x). The total revenue per product per vendor for all customers is like multiplying x * y before you do any grouping :)


1) which products a vendor offers; is this just ones that have inventory?
2) what the price of a product is; is this just last original_price paid for that product by date???
*/

-- 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 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
VALUES (7, 'Apple Pie', '10"', 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.*/



DELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp < (
SELECT MAX(snapshot_timestamp) FROM product_units WHERE product_name = 'Apple Pie');

-- 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 @@ -128,6 +175,32 @@ 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;



-- Get last quantity per vendor per product
WITH current_quantity_per_vendor_table AS (
SELECT product_id, vendor_id, quantity, market_date,
RANK() OVER (PARTITION BY product_id,vendor_id ORDER BY market_date DESC) AS last
FROM vendor_inventory
),
-- Reduce final quantities across vendors
current_quantity_table AS (
SELECT product_id, SUM(quantity) AS current_quantity
FROM current_quantity_per_vendor_table
WHERE last == 1
GROUP BY product_id
) ,
product_units_with_quantity AS (
-- keep all product_units rows; fill missing with 0
SELECT
product_id,
COALESCE(current_quantity_table.current_quantity, 0) AS current_quantity
FROM product_units
LEFT JOIN current_quantity_table USING (product_id)
)
-- update counts
UPDATE product_units
SET current_quantity = product_units_with_quantity.current_quantity
FROM product_units_with_quantity
WHERE product_units_with_quantity.product_id == product_units.product_id;</sql><current_tab id="3"/></tab_sql></sqlb_project>
Binary file added 02_activities/assignments/small bookstore.jpg
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A 'date' table is required as per question
Please also complete prompt3 (type 1 & 2 SCD)

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