[ex17] group by
ex17_group_by
select ์ปฌ๋ผ๋ฆฌ์คํธ --4. ์ปฌ๋ผ์ ์ ํ
from ํ
์ด๋ธ --1. ํ
์ด๋ธ๋ก๋ถํฐ
where ์กฐ๊ฑด --2. ์ํ๋ ๋ ์ฝ๋๋ฅผ
group by ๊ธฐ์ค --3. ๊ทธ๋ฃน์ ๋๋ ์
order by ์ ๋ ฌ; --5. ์ ๋ ฌํ๋ค.
group by์
- ๋ ์ฝ๋๋ฅผ ๋์์ผ๋ก ๊ทธ๋ฃน์ ๋๋๋ ์ญํ
- ํน์ ์ปฌ๋ผ์ ๋์์ผ๋ก ๊ฐ์ ๊ฐ์ ๊ฐ์ง๋ ๋ ์ฝ๋๋ค๋ผ๋ฆฌ ๊ทธ๋ฃน์ ๋ฌถ๋ ์ญํ
- ๊ทธ๋ฃน์ ์ ๋๋๋์ง? > ๊ฐ๊ฐ์ ๋๋ ์ง ๊ทธ๋ฃน์ ๋์ > ์ง๊ณ ํจ์๋ฅผ ์ ์ฉํ๊ธฐ ์ํด์!!!(****************)
select
round(avg(basicpay)),
buseo
from tblInsa
group by buseo;
-- ๋จ์ ๋ช๋ช
? ์ฌ์ ๋ช๋ช
? > ๋จ๋
๋ณ ๊ฐ๊ฐ ๋ช๋ช
์ธ์ง?
select * from tblComedian;
select count(*), gender
from tblComedian
group by gender;
-- ๋๋ฅ๋ณ ๊ตญ๊ฐ์?
select
count(*), continent
from tblCountry
group by continent;
select
buseo,
count(*) as "๋ถ์๋ณ ์ธ์์",
sum(basicpay) as "๋ถ์๋ณ ๊ธ์ฌ ํฉ",
round(avg(basicpay)) as "๋ถ์๋ณ ํ๊ท ๊ธ์ฌ",
max(ibsadate) as "๋ถ์๋ณ ๋ง๋ด์ ์
์ฌ์ผ",
min(ibsadate) as "๋ถ์๋ณ ๊ณ ์ฐธ์ ์
์ฌ์ผ"
from tblInsa
group by buseo;
select
gender,
round(avg(height)) as "๋จ๋
๋ณ ํ๊ท ํค",
round(avg(weight), 1) as "๋จ๋
๋ณ ํ๊ท ๋ชธ๋ฌด๊ฒ",
max(height) as "ํค๊ฐ ๊ฐ์ฅ ํฐ์ฌ๋",
min(height) as "ํค๊ฐ ๊ฐ์ฅ ์์์ฌ๋"
from tblComedian
group by gender;
--์ง์
๋ณ ์ธ์์? ๋ง์ > ์ ์
select
job,
count(*)
from tblAddressBook
group by job
--order by count(*) desc;
order by job asc;
select
round(avg(basicpay)), --์งํฉ๊ฐ(์ง๊ณํจ์)
name --๊ฐ์ธ๊ฐ(์ผ๋ฐ์ปฌ๋ผ)
from tblInsa;
--์์ ๊ฐ์ ์ํฉ์ ์๋ฌ
select
round(avg(basicpay)), --ํ๊ท ๊ธ์ฌ > ์งํฉ๊ฐ
buseo, --๋ถ์๋ช
> ํํ(๊ฐ์ธ๊ฐ์ฒ๋ผ ๋ณด์ด์ง๋ง) > ์ค์ (๊ทธ๋ฃน์ญํ ) > ์งํฉ๊ฐ
name --์ง์๋ช
> ๊ฐ์ธ๊ฐ.. ์๋ฌ
from tblInsa
group by buseo;
-- ๋ค์ค ๊ทธ๋ฃน
-- 1์ฐจ ๊ทธ๋ฃน(๋ถ์) > 2์ฐจ ๊ทธ๋ฃน(์ง์)
select
buseo as "๋ถ์๋ช
",
jikwi as "์ง์๋ช
",
count(*) as "์ธ์์"
from tblInsa
group by buseo, jikwi
order by buseo, jikwi;
-- ์ฑ๋ณ ์ธ์์?
select
count(*),
substr(ssn, 8, 1)
from tblInsa
group by substr(ssn, 8, 1);
-- ์ง์ญ๋ณ ์ธ์์?
-- ์์ธํน๋ณ์ ๊ด์ง๊ตฌ ๊ธด๊ณ ๋๋ก 13๊ธธ 5 ~ 42
-- ๊ด์ฃผ๊ด์ญ์ ์๊ตฌ ์๋ฌดํํ๋ก 62 ~ 64
-- ๊ฒฝ๊ธฐ๋ ํ์ฑ์ ๋ด๋ด์ ๋งค๋ด๋ก 123๋ฒ๊ธธ 55
select * from tblAddressBook;
select
substr(address, 1, instr(address, ' ') - 1),
count(*)
from tblAddressBook
group by substr(address, 1, instr(address, ' ')- 1);
-- ์ด๋ฉ์ผ ์ฌ์ดํธ๋ณ ์ธ์์?
select
substr(email, instr(email, '@') + 1),
count(*)
from tblAddressBook
group by substr(email, instr(email, '@') + 1)
order by count(*) desc;
-- ๊ธ์ฌ๋ณ ๊ทธ๋ฃน > ์ธ์์?
-- 100๋ง์ ์ดํ
-- 100๋ง์ ~ 200๋ง์
-- 200๋ง์ ์ด์
select
basicpay,
floor(basicpay / 1000000)
from tblInsa;
select
(floor(basicpay / 1000000) + 1) * 100 || '๋ง์ ์ดํ' as "๊ธ์ฌ๋",
count(*) as "์ธ์์"
from tblInsa
group by floor(basicpay / 1000000)
order by floor(basicpay / 1000000) asc;
-- ํ์ผ? ์ํ์ผ? ๊ฐ๊ฐ ๋ช๊ฐ?
select
count(*),
case
when completedate is null then '์ํ์ผ'
when completedate is not null then 'ํ์ผ'
end
from tblTodo
group by case
when completedate is null then '์ํ์ผ'
when completedate is not null then 'ํ์ผ'
end;
select ์ปฌ๋ผ๋ฆฌ์คํธ --5. ์ปฌ๋ผ์ ์ ํ
from ํ
์ด๋ธ --1. ํ
์ด๋ธ๋ก๋ถํฐ
where ์กฐ๊ฑด --2. ์ํ๋ ๋ ์ฝ๋๋ฅผ
group by ๊ธฐ์ค --3. ๊ทธ๋ฃน์ ๋๋ ์
having์ --4. ๊ทธ๋ฃน์ ๋ํ ์กฐ๊ฑด
order by ์ ๋ ฌ; --6. ์ ๋ ฌํ๋ค.
*** where์ vs having์
having์ (group by์ ์ปคํ)
- ์กฐ๊ฑด์
- group by์ผ๋ก๋ถํฐ ๋์จ ์
์ ๋ํ ์กฐ๊ฑด(์คํ ์์: group by > having)
- ์งํฉ์ ๋ํ ์ง๋ฌธ > ์ง๊ณ ํจ์๊ฐ์ ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ
where์ (from๊ณผ ์ปคํ)
- ์กฐ๊ฑด์
- from์ผ๋ก๋ถํฐ ๋์จ ์
์ ๋ํ ์กฐ๊ฑด(์คํ ์์: from > where)
- ๊ฐ์ธ์ ๋ํ ์ง๋ฌธ(ํ) > ์ปฌ๋ผ๊ฐ์ ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ
select๋ฌธ(***)
1. ๊ฐ ์ ์ ์ญํ
2. ๊ฐ ์ ์ ์คํ ์์
-- ๊ทธ๋ฃน(buseo) > ์ธ์์(count)
select
count(*), buseo --3. ๊ทธ๋ฃน๋ณ ์
๋ ํธ(๊ทธ๋ฃน๋ณ๋ก count์ธ๊ณ , ๋ถ์๊ฐ ๊ฐ์ ธ์ด)
from tblInsa --1. ์๋ณธ
group by buseo; --2. ๋ถ์์ปฌ๋ผ ํ์ธ, ๊ฐ์ ๊ฐ์ ๊ฐ์ง ์ ๋ค๋ผ๋ฆฌ ๊ทธ๋ฃน ํ์ฑ
-- ์์ธ ์ฌ๋ ์ง์ > ๋ถ์๋ณ ์ธ์์?
select
count(*), buseo --4.
from tblInsa --1. ํ
์ด๋ธ ์๋ณธ
where city = '์์ธ' --2. ํ์ ๋ํ ์กฐ๊ฑด ํํฐ
group by buseo; --3. ๋ถ์๋ก ๊ทธ๋ฃน ๋๋
select
count(*), buseo --4.
from tblInsa --1.
where basicpay >= 2500000 --2. ์ฌ๊ธฐ์ ์กฐ๊ฑด ์๋๋ ๊ฒฝ์ฐ ํ๋ฝ(๋ถ์ ๋ช๊ฐ ์์ด์ง)
group by buseo; --3.
-- having์
select
buseo,
round(avg(basicpay)) --3. ๋๋ ์ง ๊ทธ๋ฃน๋ณ๋ก ์ง๊ณํจ์๋ฅผ ๊ฐ๊ฐ ๊ตฌํ๋ค.
from tblInsa --1. 60๋ช
์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
group by buseo; --2. 60๋ช
์ ๋์์ผ๋ก > ๋ถ์๋ก ๊ทธ๋ฃน์ ๋๋๋ค.
select
buseo,
round(avg(basicpay)) --4. ๋๋ ์ง ๊ทธ๋ฃน๋ณ๋ก ์ง๊ณํจ์๋ฅผ ๊ฐ๊ฐ ๊ตฌํ๋ค.
from tblInsa --1. 60๋ช
์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
where basicpay >= 1500000 --2. 60๋ช
์ ๋์์ผ๋ก ์กฐ๊ฑด์ ๋ง๋ ์ง์๋ง ๋จ๊ธด๋ค.
group by buseo; --3. where์ ์ ๋ง์กฑํ ์ง์๋ค์ ๋์์ผ๋ก ๊ทธ๋ฃน์ ๋๋๋ค.
-- where์ ์ ๊ฐ์ธ์ ๋ํ ์กฐ๊ฑด์ด๊ธฐ๋๋ฌธ์ ์ง๊ณํจ์ ์ฌ์ฉX
-- having์ ์ ๊ฐ๋ฅ
select
buseo,
round(avg(basicpay)) --4. ๋๋ ์ง ๊ทธ๋ฃน๋ณ๋ก ์ง๊ณํจ์๋ฅผ ๊ฐ๊ฐ ๊ตฌํ๋ค.
from tblInsa --1. 60๋ช
์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
group by buseo --2. 60๋ช
์ ๋์์ผ๋ก > ๋ถ์๋ก ๊ทธ๋ฃน์ ๋๋๋ค.
having avg(basicpay) >= 1500000; --3. ๊ทธ๋ฃน๋ณ ์ง๊ณํจ์๊ฐ์ ์กฐ๊ฑด์ผ๋ก ํํฐ๋ง
select
buseo,
round(avg(basicpay)) --5.
from tblInsa --1.
where basicpay >= 1500000 --2.
group by buseo --3.
having avg(basicpay) >= 2200000 --4.
order by avg(basicpay) desc; --6.
group by ํจ์
1. rollup()
- group by ๊ฒฐ๊ณผ์์ ์ง๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ ์์ธํ๊ฒ ๋ฐํ
2. cube()
- group by ๊ฒฐ๊ณผ์์ ์ง๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ ์์ธํ๊ฒ ๋ฐํ
- rollup() ๋ณด๋ค ์ข ๋ ์์ธํ๊ฒ ํํ
- rollup()์ ๋นํด ์ข ๋ ๋ค์ํ ๊ธฐ์ค์ผ๋ก ์ค๊ฐ ์ง๊ณ ์ถ๊ฐ
-- rollup()
select
buseo,
jikwi,
count(*),
round(avg(basicpay))
from tblInsa
group by rollup(buseo, jikwi);
select
buseo,
jikwi,
city,
count(*),
round(avg(basicpay))
from tblInsa
group by rollup(buseo, jikwi, city);
--cube()
select
buseo,
jikwi,
count(*),
round(avg(basicpay))
from tblInsa
group by cube(buseo, jikwi);
select
buseo,
jikwi,
city,
count(*),
round(avg(basicpay))
from tblInsa
group by cube(buseo, jikwi, city);