katiehuangx / 8-Week-SQL-Challenge

Case study solutions for the #8WeekSQLChallenge.
https://8weeksqlchallenge.com
226 stars 161 forks source link

In Case Study #1: Danny's Diner it should be using row_number instead of dense_rank in below question due to which 2 products are displayed because of dense_rank #5

Closed shubhamphegade closed 1 year ago

shubhamphegade commented 1 year ago

@katiehuangx --3. What was the first item from the menu purchased by each customer? WITH ordered_sales_cte AS

(

SELECT 
customer_id, 
order_date, 
product_name,
    row_number() OVER(PARTITION BY s.customer_id ORDER BY s.order_date) AS rank
FROM dbo.sales AS s
JOIN dbo.menu AS m
    ON s.product_id = m.product_id

)

SELECT customer_id, product_name FROM ordered_sales_cte WHERE rank = 1 GROUP BY customer_id, product_name;

--Using dense_rank: image

--Using row_number(correct one): image

jegazhu commented 1 year ago

Great sharing, thanks.

raghavbansal99 commented 1 year ago

Since both 'sushi' and 'curry' are puchased on the same day (2021-01-01), they should both be included

katiehuangx commented 1 year ago

The order_date field does not have a timestamp, hence it is impossible to determine the exact sequence of items ordered by the customer.

Therefore, it would be inaccurate to conclude that curry is the customer's first order purely based on the alphabetical order of the product names.

For this reason, I maintain my solution of using DENSE_RANK() and consider both curry and sushi as Customer A's first order.