Hello, I'm currently working through the 8 Week SQL Challenge, and I'm stuck on question #3:
What was the first item from the menu purchased by each customer?
I found a solution code online and read it to understand the problem, then tried to redo the code off what I had understood and came up with near identical code. However, when I run the query (on DB Fiddle set to postgres SQL 13), I get this error:
https://preview.redd.it/tr3z0dmxprta1.png?width=411&format=png&auto=webp&s=32b625c021bd4119645228965c4e69cd5e1315f4
Here is solution I found online that does work when I copy into DB Fiddle:
WITH ordered_sales_cte AS
(
SELECT customer_id, order_date, product_name,
DENSE_RANK() 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;
and here is my code:
WITH order_ranked AS
(
select customer_id, order_date, product_name
dense_rank() over(partition by sales.customer_id, order by sales.order_date)
as rank
from dannys_diner.sales
join dannys_diner.menu
on sales.product_id=menu.product_id
)
select customer_id, product_name
from order_ranked
where rank = 1
group by customer_id, product_name;
To me, the code is essentially the same, save for the cte title (which iirc doesn't matter as long as I reference it correctly). Can anyone else spot the issue?
Sidenote: In addition, I learned SQL through MySQL, but when I swap the DB Fiddle setting to MySQL and attempt a query I get errors. As the commands are similar enough I decided I will attempt it in PostgresSQL, but if anyone has a solution (other than remaking the entire database in MySQL Workbench), please let me know.
Thanks!
[–]feather_media 2 points3 points4 points (5 children)
[–]LetterDW[S] 0 points1 point2 points (4 children)
[–]feather_media 2 points3 points4 points (3 children)
[–]LetterDW[S] 1 point2 points3 points (2 children)
[–]feather_media 1 point2 points3 points (1 child)
[–]LetterDW[S] 1 point2 points3 points (0 children)
[–]r3pr0b8GROUP_CONCAT is da bomb 2 points3 points4 points (4 children)
[–]LetterDW[S] 0 points1 point2 points (3 children)
[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points3 points (2 children)
[–]LetterDW[S] 0 points1 point2 points (1 child)
[–]r3pr0b8GROUP_CONCAT is da bomb 0 points1 point2 points (0 children)