Day16. ์คํ ๋์ ๋ฌ ์๊ฐ ์ฐพ๊ธฐ
https://solvesql.com/problems/find-steadyseller-writers/
with t1 as (
select author, year
from books
where genre = 'Fiction'
group by author, year
order by author, year
), t2 as (
select author
, year as p1
, lead(year,4) over(partition by author order by year asc) as p5
from t1
)
select author, max(p1) as year , count(DISTINCT p1) as depth
from t2
where author in (select DISTINCT author
from t2
where p5 is not null
and p1+4 = p5)
group by author
โWHERE ์ ์ ์๋ธ์ฟผ๋ฆฌ๋ก ์กฐ๊ฑด ๋๊ธฐ
โ์๋ธ์ฟผ๋ฆฌ : P5๊ฐ ๋น์ด์์ง ์๊ณ ์ฐ์์ธ ๊ฒ)
+) WITH ์ : T1
โWHERE ์ ์ ์ฅ๋ฅด ์กฐ๊ฑด ๊ฑธ๊ธฐ
โGROUP BY ๋ก ์๊ฐ์ ๋
๋ ์ง์ ํ๊ธฐ
+) WITH ์ : T2
โLEAD() ์ ์๋์ฐ ํจ์ ์ฌ์ฉํด์ 4๋
๋ค์ ๊ฒ ์นผ๋ผ ์ง์ ํ๊ธฐ
-> 5๋
์ด์ ๋ฒ ์คํธ ์
๋ฌ์ธ ๊ฒ ํ๊ธฐ