DAY-24. Oracle group by, having
๐ 2022-04-04 - 1
๋ฐ์ดํฐ๋ฒ ์ด์ค
โ ์์ ์ ๋ฃ๊ณ , ๊ฐ์ธ์ด ๊ณต๋ถํ ๋ด์ฉ์ ์ ์ ๊ฒ ์ด๊ธฐ์ ์ค๋ฅ๊ฐ ๋ง์ ์๋ ์์
1๏ธโฃ group by
- ๊ทธ๋ฃนํจ์ ๋จ ํ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๋์ถํ๊ณ ์ถ์ ๋(๊ทธ๋ฃน๋ณ ์ง๊ณ)
- ๊ทธ๋ฃนํจ์๊ฐ ์ ์ฉ๋ ๊ทธ๋ฃน ๊ธฐ์ค์ผ๋ก ํด์ group by ์ ์ ํด๋น ๊ทธ๋ฃน์(์ปฌ๋ผ) ๊ธฐ์ ํด ์ฌ์ฉ
- ๊ทธ๋ฃน์ผ๋ก ๋๋์ด์ ๊ฒฐ๊ณผ ๋์ถํ๋ค๊ณ ์๊ฐ
๐
//๋ถ์๋ณ ์ธ์ ์กฐํ
select nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋" , count(*) from employee
group by dept_code;
๐
// ๋ถ์๋ณ ์ธ์ ์กฐํ -> ๋ถ์์ฝ๋๊ฐ null ์ธ ๊ฒฝ์ฐ์๋ count๋ฅผ ํ์ง ์์
select dept_code "๋ถ์์ฝ๋", count(*) from employee
where dept_code is not null
group by dept_code;
๐
//๋ถ์์ฝ๋, ๋ถ์๋ณ ์ธ์, ๋ถ์๋ณ ์๊ธ ์ดํฉ, ๋ถ์๋ณ ์๊ธ ํ๊ท ์กฐํ
select nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋" ,
count(*) "๋ถ์๋ณ ์ธ์",
to_char(sum(salary),'L999,999,999') "๋ถ์๋ณ ์๊ธ ์ดํฉ",
to_char(round(avg(salary)),'L999,999,999') "๋ถ์๋ณ ์๊ธ ํ๊ท "
from employee
group by dept_code
order by 1;
๐
//๋ถ์์ฝ๋, ๋ณด๋์ค๋ฅผ ์ง๊ธ๋ฐ๋ ์ฌ์์ ์(๋ณด๋์ค์จ์ด ์๋ ์ฌ์)๋ฅผ ์กฐํ -> ๋ถ์์ฝ๋ ์์ผ๋ก ์ ๋ ฌ
select nvl(dept_code,'์ธํด')"๋ถ์์ฝ๋",
count(*) || '๋ช
' "๋ณด๋์ค ๋ฐ๋ ์ฌ์์"
from employee
where bonus is not null
group by dept_code
order by 1;
๐
// ์ฑ๋ณ, ์ฑ๋ณ๋ณ ๊ธ์ฌ์ ํ๊ท (์ ์์ฒ๋ฆฌ_์์์ ๋ฒ๋ฆผ), ์ฑ๋ณ๋ณ ๊ธ์ฌ์ ํฉ๊ณ, ์ฑ๋ณ๋ณ ์ธ์์ -> ์ธ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์
select
decode(substr(emp_no, 8, 1),'1','๋จ','์ฌ') "์ฑ๋ณ",
to_char(floor(avg(salary)),'L999,999,999')"๊ธ์ฌ ํ๊ท ",
to_char(sum(salary),'L999,999,999') "๊ธ์ฌ ํฉ๊ณ ",
count(*) "์ธ์"
from employee
group by substr(emp_no, 8, 1)
order by 4 desc;
- group by์ extract๋ฅผ ์์ฐ๊ณ hire_date๋ก ํ๋ฉด ๋ ๋๋ ๊ฐ์๋ฐ ๋ ์ง๊ฐ ๋ค๋ฅด๋ฉด ๋ค๋ฅธ ๊ทธ๋ฃน์ผ๋ก ๋ฐ๋์ด๋ฒ๋ฆฐ๋ค ๊ทธ๋์ extract์ฌ์ฉ ํ์
group by ์์ 1๊ฐ ์ด์์ ์กฐ๊ฑด
- ๊ทธ๋ฃน ์์ ๊ทธ๋ฃน โ ๊ทธ๋ฃน์ ๋จผ์ ํ๋์ ์กฐ๊ฑด์ผ๋ก ๋๋๋ค, ๊ทธ ์์์ ๋ ๋ค๋ฅธ ์กฐ๊ฑด์ผ๋ก ๋๋
๐
//๊ฐ ๋ถ์์์ ์ง๊ธ๋ณ๋ก ๋ถ์์ฝ๋, ์ง๊ธ์ฝ๋, ์ง๊ธ๋ณ ์ฌ์์, ํ๊ท ๊ธ์ฌ
select
nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋",
job_code "์ง๊ธ์ฝ๋",
count(*) "์ฌ์์",
round(avg(salary)) "ํ๊ท ๊ธ์ฌ"
from employee
group by dept_code,job_code
order by 1,2;
๐
//๋ฌธ์ 3. ๋ถ์์ฝ๋, ์ฑ๋ณ, ๋ถ์๋ณ ์ฑ๋ณ ์ฌ์์๋ฅผ ๊ตฌํ์ธ์.
(๋ถ์์ฝ๋, ์ฑ๋ณ ๊ธฐ์ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ)
select nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋",
decode(substr(emp_no,8,1),'1','๋จ','์ฌ') "์ฑ๋ณ",
count(*)
from employee
group by dept_code, substr(emp_no,8,1) -- ๋จผ์ ๋ถ์๋ก ๋๋๊ณ , ๊ทธ ๋ค์ ์ฑ๋ณ๋ก ๋๋
order by dept_code ,2 ;
2๏ธโฃ Having
- where์ ์์ groupํจ์๋ฅผ ์ฌ์ฉ ํ ์ ์์ ๋
- group by๋ฅผ ์ด์ฉํด์ ๊ทธ๋ฃนํจ์๋ก ๊ฐ์ ๊ตฌํด์ฌ ๋ ๊ทธ ๊ฐ์ ๋ํ ์กฐ๊ฑด์ ๊ฑธ๊ณ ์ถ๋ค๋ฉด ์ฌ์ฉํ๋ ์
- ์ฆ having์ group by๋ฅผ ํตํด ๋์จ ๊ฐ์ ๋ค์ ์กฐ๊ฑด์ ์ค ๋ ์ฌ์ฉ
๐
// ๊ธ์ฌ ํ๊ท ์ด 300๋ง์ ์ด์์ธ ๋ถ์์ ๋ํ ๋ถ์์ฝ๋, ํ๊ท ๊ธ์ฌ ์กฐํ
// ๋ถ์๋ณ๋ก ๊ทธ๋ฃนํ -> ๊ฐ ๋ถ์๋ณ ๊ธ์ฌ ํ๊ท -> ๊ทธ ํ๊ท ์ด 300๋ง์ ์ด์ -> ๋ถ์์ฝ๋, ํ๊ท ๊ธ์ฌ ์กฐํ
// ๋ถ์์ฝ๋๊ฐ D6๋ผ๋ฉด ์ ์ธ
select nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋",
floor(avg(salary)) "ํ๊ท ๊ธ์ฌ"
from employee
where dept_code not in ('D6')
group by dept_code
having floor(avg(salary)) >= 3000000;
๐
//์ธ์์ด 3๋ช
์ด ๋ฏธ๋ง์ธ ์ง๊ธ์ฝ๋๋ ์ธ์์ ๋ณด์ถฉ
select nvl(job_code,'์ธํด') "์ง๊ธ์ฝ๋",
count(*) "์ธ์"
from employee
group by job_code
having count(*) < 3; -- group by์ ๊ฒฐ๊ณผ ๊ฐ์ ์กฐ๊ฑด์ ๊ฑฐ๋๊ฑฐ๋ค
๐ก where VS having
- where์ ๊ธฐ๋ณธ์ ์ธ ์กฐ๊ฑด์ ๋ก์ ์ฐ์ ์ ์ผ๋ก ๋ชจ๋ ํ๋๋ฅผ ์กฐ๊ฑด์ ๋ ์ ์๋ค. ํ์ง๋ง having์ group by ๋ ์ดํ ํน์ ํ ํ๋๋ก ๊ทธ๋ฃนํ ๋์ด์ง ์๋ก์ด ํ ์ด๋ธ์ ์กฐ๊ฑด์ ์ค ์ ์๋ค.
1) select dept_code, salary, from employee where salary > 4000000 group by dept_code;
2) select dept_code, avg(salary) from employee where salary group by dept_code having avg(salary)> 4000000;
1)์ ๊ฒฝ์ฐ๋ where ์กฐ๊ฑด์ ์ ๋ง๋ ์ ๋ณด๋ฅผ ์ฐ์ ์ ์ผ๋ก ๋ค๊ณ ์์ group by๋ก ๊ทธ๋ฃนํ ํ๋ค
2)์ ๊ฒฝ์ฐ๋ group by๋ก ๊ทธ๋ฃนํ ํ ์ํ์์ having ์กฐ๊ฑด์ ์ ๋ง๋ ์ ๋ณด๋ฅผ ๋ค๊ณ ์จ๋ค
Leave a comment