DAY 5: PROSQL45 CHALLENGE
Suparna Chowdhury
Data Scientist | Python, SQL, and Tableau Expert | Driving Data Insights
A big thanks to Ankit Bansal the year-end challenge! I’m taking it on by solving 2 complex SQL questions every day from a playlist of 79 problems.
PROBLEM 9:
This problem is sourced from Leetcode.com : https://leetcode.ca/all/1159.html?
There are three tables: Users, Orders, and Items. The Users table contains user details, the Orders table records transactions, and the Items table has details of the items sold.
Write an SQL query to determine for each user whether the brand of the second item they sold (ordered by the sale date) matches their favorite brand. If a user sold fewer than two items, return "no" for that user.
Video Solution by Ankit: https://www.youtube.com/watch?v=1ias-sP_XAY&list=PLBTZqjSKn0IeKBQDjLmzisazhqQy4iGkb&index=9
?
Step-by-Step Solution Approach:
CODE:
WITH ranked_orders
AS (SELECT o.seller_id,
o.order_date,
i.item_brand,
Row_number()
OVER (
partition BY o.seller_id
ORDER BY o.order_date) AS rn
FROM orders o
LEFT JOIN items i
ON o.item_id = i.item_id)
SELECT u.user_id AS seller_id,
COALESCE(CASE WHEN u.favorite_brand = ro.item_brand
THEN 'Yes' ELSE 'No' END, 'No')
AS '2nd_item_fav_brand'
FROM users u
LEFT JOIN ranked_orders ro
ON u.user_id = ro.seller_id
WHERE ro.rn = 2
OR ro.rn IS NULL ?
PROBLEM 10:
领英推荐
Question: Given the tasks table with columns date_value and state, write an SQL query to find the start date, end date, and state of tasks. The start date should be the first date for each state group (e.g., the first occurrence of 'success' or 'fail'), and the end date should be the last date for each state group. The query should return the results grouped by the task's state, and for each state, the corresponding start and end dates.
Video Solution by Ankit: https://www.youtube.com/watch?v=WrToXXN7Jb4&list=PLBTZqjSKn0IeKBQDjLmzisazhqQy4iGkb&index=10
Step-by-Step Solution Approach:
CODE:
With cte AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY state ORDER BY date_value) AS rn,
DATEADD(DAY, -1 * ROW_NUMBER() OVER(PARTITION BY state ORDER BY date_value), date_value) AS group_date
FROM tasks
ORDER BY date_value)
SELECT MIN(date_value) AS start_date, MAX(date_value) AS end_date, state
FROM cte
GROUP BY state, group_date
ORDER BY start_date
?
Let’s level up our SQL skills!
Join me on this journey as I tackle more challenges each day. Let’s improve our SQL skills together before the year ends!
?
Check out the full solutions on GitHub here!
?
Data Scientist | Python, SQL, and Tableau Expert | Driving Data Insights
3 个月Want to see the full solution? Check it out on my GitHub : https://github.com/suchow07/Complex_SQL/blob/main/DAY_5.sql