Skip to content
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

UofT-DSI | sql - homework 4 #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
96 changes: 90 additions & 6 deletions 02_activities/homework/homework_4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ 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
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_details
FROM product;

--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -30,16 +31,52 @@ 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. */

-- Query to reverse the numbering of each customer's visit
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number
FROM
customer_purchases;

-- Query to filter only the most recent visit of each customer using a subquery
SELECT
customer_id,
market_date AS most_recent_visit
FROM
(
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number
FROM
customer_purchases
) AS ranked_visits
WHERE
visit_number = 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,
market_date,
COUNT(product_id) OVER (PARTITION BY customer_id, product_id) AS purchases_count
FROM
customer_purchases;


-- String manipulations
Expand All @@ -54,11 +91,24 @@ 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 RTRIM(LTRIM(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
product_name,
product_size
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.
Expand All @@ -70,6 +120,40 @@ 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_by_date AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales_value
FROM
customer_purchases
GROUP BY
market_date
),
ranked_sales AS (
SELECT
market_date,
total_sales_value,
RANK() OVER (ORDER BY total_sales_value DESC) AS best_day_rank,
RANK() OVER (ORDER BY total_sales_value ASC) AS worst_day_rank
FROM
sales_by_date
)
SELECT
'Best Day' AS type_of_day,
market_date,
total_sales_value
FROM
ranked_sales
WHERE
best_day_rank = 1

UNION ALL


SELECT
'Worst Day' AS type_of_day,
market_date,
total_sales_value
FROM
ranked_sales
WHERE
worst_day_rank = 1;