카타
문제 116. 1321. Restaurant Growth : 윈도우 함수-집계 행 지정, limit 활용(offsetX)
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
with cte as
(select visited_on,
sum(amount) as amount
from customer
group by visited_on)
select visited_on,
sum(amount) over (order by visited_on rows between 6 preceding and CURRENT ROW) as amount,
round(avg(amount*1.00) over (order by visited_on rows between 6 preceding and current row), 2) as average_amount
from cte
order by visited_on
limit 6, 18446744073709551610;
포인트
1. 윈도우 함수 행 지정 익혀 둘 것!
-sum() over (order by ~ rows between ~ preceding and current row)
2. 다른 답안에는 offset이 있는데 왜인지 offset이 사용이 되질 않았다;;
'TIL 통합 > SQL' 카테고리의 다른 글
정규 표현식 (0) | 2024.01.31 |
---|---|
INTERSECT, GROUP_CONCAT, 집계함수 (0) | 2024.01.30 |
Exchange Seats if(ifnull) 및 case when 조건문 동시 활용, union all (0) | 2024.01.26 |
1. WHERE, UNION, DISTINCT, 2. WITH, 윈도우함수 (0) | 2024.01.25 |
SQL 1/24 - where, union, lead/lag (0) | 2024.01.24 |