리뷰 : 행의 범위를 지정할 때 PRECEDING, CURRENT ROW, FOLLOWING을 잘 활용하자. UNBOUNDED는 무한하다는 뜻.
그리고 두번째 문제에서는 상관이 없지만 알아두면 좋은 것 :
RANK() => 값이 같으면 같은 순위
DENSE_RANK() => 값이 같아도 순위가 다름
ROW_NUMBER() => 순번을 매긴다 (RANK와 유사)
ROW_NUMBER() OVER(ORDER BY (SELECT 1))
위와 같이 코딩하면 조회된 순서대로 번호를 매길 수 있다.
URL : https://datalemur.com/questions/rolling-average-tweets

SELECT
user_id
,tweet_date
,ROUND(AVG(tweet_count)
OVER(PARTITION BY user_id ORDER BY tweet_date ROWS BETWEEN 2 PRECEDING and CURRENT ROW),2)
FROM tweets;
URL : https://datalemur.com/questions/sql-highest-grossing

SELECT
category
,product
,total_spend
FROM (
SELECT
category
,product
,SUM(spend) total_spend
,ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(spend) DESC) row_num
FROM product_spend
WHERE EXTRACT(YEAR FROM transaction_date) = 2022
GROUP BY 1,2
) a
WHERE row_num < 3