Day 17. ๋ฉํฐ ํ๋ซํผ ๊ฒ์ ์ฐพ๊ธฐ
https://solvesql.com/problems/multiplatform-games/
-- ๊ฐ์ ์ด๋ฆ์ด์ด๋ ๋
๋๋ค ์์ด๋๊ฐ ๋ค๋ฆ
with t1 as (
select g.game_id, g.name as game_name
, year
, p.platform_id, p.name as platform_name
, case
when p.name in ('PS3', 'PS4', 'PSP', 'PSV') then 'Sony'
when p.name in ('Wii', 'WiiU', 'DS', '3DS') then 'Nintendo'
when p.name in ('X360', 'XONE') then 'Microsoft'
end as 'major_p'
from games as g
left join platforms as p on g.platform_id = p.platform_id
where year >= 2012
order by g.name, year
), t2 as (
select game_name, major_p
, count(major_p) over(PARTITION by game_name) as major_cnt
from t1
where major_p is not null
group by game_name, major_p
)
select DISTINCT game_name as name
from t2
where major_cnt >=2
โWHERE ์ ์ ๋ฉ์ด์ ํ๋ซํผ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ธ ๊ฒ๋ง ์ถ์ถ
+) WITH ์ : T1
โCASE WHEN ์ ์ ๊ฒ์๊ธฐ์ ๋ฐ๋ผ ํ๋ซํผ ์นผ๋ผ ๋ง๋ค๊ธฐ
โLEFT JOIN ์ฌ์ฉํด์ ๊ฒ์๊ณผ ํ๋ซํผ ์ฐ๊ฒฐ
+) WITH ์ : T2
โ์๋์ฐ ํจ์ ์ฌ์ฉํด์ ๊ฒ์ ์ด๋ฆ์ ๋ฐ๋ผ ๋ฉ์ด์ ํ๋ซํผ ๊ฐฏ์ ์ธ๊ธฐ
โWHERE ์ ์ ๋ฉ์ด์ ํ๋ซํผ์ธ ๊ฒ๋ง ์ฌ์ฉ
โGROUP BY ์ฌ์ฉํด์ ๊ฒ์ ์ด๋ฆ๊ณผ ๋ฉ์ด์ ํ๋ซํผ ๊ธฐ์ค ๋ง๋ค๊ธฐ