๋ฌธ์
https://solvesql.com/problems/find-steadyseller-writers/
https://solvesql.com/problems/find-steadyseller-writers/
solvesql.com
์ฝ๋ ๋ต์
ํ ๋ธ๋ก๊ทธ ํ์ด
with fictions as (
select DISTINCT
author,
year
from
books
where
genre = "Fiction"
order by 1,2
)
select author, max(year) as year, count(*) as depth
from
(select *, year - row_number() over () as grp_num
from fictions) as t
group by author, grp_num
having count(*) >= 5
๋ฌธ์ ํฌ์ธํธ
1. ์ฅ๋ฅด๊ฐ "Fiction"์ธ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋ง
2. ๋ ๋์์ row_number๋ฅผ ๋นผ์ ์ฐ์์ ์ธ ๋ ๋๋ฅผ ๊ตฌํ๋ค
๋ ๋์์ row_number๋ฅผ ๋บ์ ๋ ์ฐ์์ ์ธ ๋ ๋์ด๋ฉด ๊ฐ์ ์ซ์ (grp_num)์ ๋ถ์ฌ๋ฐ๊ฒ ๋๋ค.
grp_num์ ๊ธฐ์ค์ผ๋ก ๋ค์ ๊ทธ๋ฃนํ ํ count(*)์ ํ๋ฉด ์ฐ์์ ์ธ ํ์ ๊ฐ์๋ฅผ ๊ตฌํ ์ ์๋ค.
3. ์ฐ์์ ์ธ ํ์ด 5๊ฐ ์ด์์ธ grp_num์์ ๊ฐ์ฅ ์ต์ ๋ ๋๋ฅผ ๊ตฌํ๋ค.
๋๋ ์
์์ ์๋ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฐ์์ ์ธ ์ซ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๊ฐ ๋์์๋๋ฐ
์จ๋จน์ ์๊ฐ์ ๋ชปํ๋ค ใ
๋ณต์ต์ค์ !
