๋ฌธ์
https://leetcode.com/problems/count-salary-categories/
์ฝ๋
ํ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ ํ์ด
select 'Low Salary' as category, sum(if(income < 20000, 1, 0)) as accounts_count
from accounts
union
select 'Average Salary' as category, sum(if(income between 20000 and 50000, 1, 0)) as accounts_count
from accounts
union
select 'High Salary' as category, sum(if(income >= 50000, 1, 0)) as accounts_count
from accounts
๋ฌธ์ ํ์ด
case when๊ณผ group by๋ก ๋ฌธ์ ๋ฅผ ์ ๊ทผํ๋ฉด
average salary์ ํด๋นํ๋ id๋ ์๊ธฐ ๋๋ฌธ์ 0์ด ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ ์๋๋ผ ์์ ์๋ต์ด ๋๋ค.
๋ฐ๋ผ์ ์ด ๋ฌธ์ ์ ๊ด๊ฑด์ ๊ฐ์ด 0์ธ average salary๋ ๊ฒฐ๊ณผ์ ๋ํ๋๊ฒ ํด์ผ ํ๋ ๊ฒ์ด๋ค.
๊ทธ ๋ฐฉ๋ฒ์ผ๋ก union์ ์ฌ์ฉํ ๊ฒ์ด๋ค
low salary, avaerage salary, high salary์ ์ง๊ณํ ํ ์ด๋ธ 3๊ฐ๋ฅผ ๊ฐ๊ฐ ๋ง๋ค์ด์ฃผ๊ณ
union์ผ๋ก ํ ์ด๋ธ์ ํฉ์ณ์ฃผ๋ฉด ๋๋ค!
๋๋ ์
category ์ปฌ๋ผ์ ์ง์ ๊ฐ์ ๋ฃ์ด์ ๋ง๋ค์ด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ๋๋ฐ
๋๋ ํ๊บผ๋ฒ์ 3๊ฐ์ ํ์ ๋ํ ๊ฐ์ ์ง์ด๋ฃ์ผ๋ ค๊ณ ํ๋ค. (๊ทธ๋ฐ ๋ฐฉ๋ฒ์ ์๋ ๋ฏ?)
์ปฌ๋ผ์ ์ง์ ๊ฐ์ ๋ฃ์ ๋๋ ๋จ์ผํ๋ง ๊ฐ๋ฅํ๊ณ
union์ ์ฌ์ฉํด์ ๋ค์คํ์ผ๋ก ๋ง๋ค์ด์ฃผ๋ฉด ๋๊ฒ ๋ค.
๊ทธ๋ฆฌ๊ณ group by ์ง๊ณ๋ ๊ฐ์ด 0์ธ ๊ฒ์ ์ถ๋ ฅ์ ์ํด์ค๋ค ! (๋ช ์ฌ ๋ ๋ช ์ฌ ~)
