본문 바로가기

TIL 통합/SQL

윈도우 함수-집계 행 지정

카타

 

문제 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이 사용이 되질 않았다;;