문제 1. Highest-Grossing Items
Assume you're given a table containing data on Amazon customers and their spending on products in different category, write a query to identify the top two highest-grossing products within each category in the year 2022. The output should include the category, product, and total spend.
https://datalemur.com/questions/sql-highest-grossing
Amazon SQL Interview Question | DataLemur
Amazon SQL Interview Question: Write a query to identify highest-grossing products within each category.
datalemur.com
💡 문제 풀이
`내 풀이`
소요 시간 08:06
with t1 as(
SELECT category, product,
sum(spend) AS total_spend,
rank() over(partition by category order by sum(spend) desc) AS ranking
FROM product_spend
WHERE EXTRACT (YEAR FROM transaction_date) = 2022
GROUP BY category, product
)
SELECT category, product, total_spend
FROM t1
WHERE ranking < 3;
`오답노트`
- 자꾸 맨 뒤에 세미콜론 ; 빼먹는데 습관 들이기
💯결과