-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment two #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,12 @@ 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') || ')' | ||
| AS ProductListForManager | ||
| FROM product | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -41,8 +44,13 @@ 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, | ||
| ROW_NUMBER () OVER ( | ||
| PARTITION BY customer_id | ||
| ORDER BY market_date ASC) AS VisitNumber | ||
| FROM customer_purchases | ||
| WHERE market_date < '2022-04-29'; | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -52,9 +60,13 @@ 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, VisitNumber | ||
| FROM (SELECT customer_id, market_date, | ||
| ROW_NUMBER () OVER ( | ||
| PARTITION BY customer_id | ||
| ORDER BY market_date DESC) AS VisitNumber | ||
| FROM customer_purchases) | ||
| AS MostRecentVisit WHERE VisitNumber = 1; | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -66,8 +78,13 @@ 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 DISTINCT | ||
| customer_id, product_id, | ||
| COUNT (*) OVER ( | ||
| PARTITION BY product_id , customer_id | ||
| ORDER BY product_id ASC) AS CustomerHasBoughtProductNTimes | ||
| FROM customer_purchases | ||
| WHERE market_date < '2022-04-29'; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DISTINCT not required here. |
||
| --END QUERY | ||
|
|
||
|
|
@@ -84,18 +101,21 @@ 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 *, | ||
| TRIM(NULLIF(SUBSTR(product_name, INSTR(product_name, '-') + 1), 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 * | ||
| FROM( | ||
| SELECT *, | ||
| TRIM(NULLIF(SUBSTR(product_name, INSTR(product_name, '-') + 1), product_name)) AS Description | ||
| FROM product) | ||
| WHERE product_size REGEXP '[0-9]'; | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -110,9 +130,19 @@ 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 | ||
|
|
||
|
|
||
|
|
||
| SELECT market_date, Min(TotalSalePerDay) AS TotalSale, 'MinSaleDay' AS MinMaxSaleDay | ||
| FROM( | ||
| SELECT quantity, cost_to_customer_per_qty, market_date, | ||
| SUM( quantity*cost_to_customer_per_qty) AS TotalSalePerDay | ||
| FROM customer_purchases | ||
| GROUP BY market_date) AS LowestSaleDate | ||
| UNION | ||
| SELECT market_date, Max(TotalSalePerDay) AS TotalSale, 'MaxSaleDay' AS MinMaxSaleDay | ||
| FROM( | ||
| SELECT quantity, cost_to_customer_per_qty, market_date, | ||
| SUM( quantity*cost_to_customer_per_qty) AS TotalSalePerDay | ||
| FROM customer_purchases | ||
| GROUP BY market_date) | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -131,9 +161,13 @@ 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 vendor.vendor_name, product.product_name, temptable.FiveProductsSoledPrice | ||
| FROM( | ||
| SELECT vendor_id, product_id, 5*(product_id*original_price) AS FiveProductsSoledPrice | ||
| FROM vendor_inventory | ||
| GROUP BY product_id) AS temptable | ||
| JOIN vendor ON temptable.vendor_id = vendor .vendor_id | ||
| JOIN product ON temptable.product_id = product.product_id; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The requirement here is to use CROSS JOIN. |
||
| --END QUERY | ||
|
|
||
|
|
@@ -144,19 +178,20 @@ 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 | ||
| VALUES ('30', 'ApplePie', 'small', '3', 'unit', CURRENT_TIMESTAMP); | ||
|
|
||
| --END QUERY | ||
|
|
||
|
|
@@ -166,9 +201,8 @@ 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 snapshot_timestamp = (SELECT MAX(snapshot_timestamp) FROM product_units ); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also want to add condition to check product_id to identify the product. |
||
| --END QUERY | ||
|
|
||
|
|
@@ -191,7 +225,21 @@ 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 = ( | ||
| SELECT COALESCE(quantity, 0) | ||
| FROM vendor_inventory | ||
| WHERE vendor_inventory.product_id = product_units.product_id | ||
| AND market_date = ( | ||
| SELECT MAX(market_date) | ||
| FROM vendor_inventory AS temptable | ||
| WHERE temptable.product_id = product_units.product_id | ||
| ) | ||
| ) | ||
| WHERE product_units.product_id IN (SELECT product_id FROM vendor_inventory); | ||
|
|
||
|
|
||
| --END QUERY | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GROUP BY customer_id, market_daterequired here.