[ex08~09] ์ง๊ณํจ์(aggregation function), ์ํ ํจ์(numerical_function)
ex08_aggregation_function
์ง๊ณํจ์, Agregation_function
1. count()
2. sum()
3. avg()
4. max()
5. min()
- ์ง๊ณ ํจ์ ์ฌ์ฉ ์ฃผ์์ !!!
-- 1. ORA-00937: not a single-group group function
-- ์ปฌ๋ผ ๋ฆฌ์คํธ์ ์ง๊ณ ํจ์์ ์ผ๋ฐ ์ปฌ๋ผ์ ๋์์ ์ฌ์ฉํ ์ ์๋ค.
-- ์ง๊ณ ํจ์ ๋ฐํ๊ฐ(์งํฉ), ์ปฌ๋ผ(๊ฐ์ธ)
--์๊ตฌ์ฌํญ] ์ง์๋ค์ ์ด๋ฆ๊ณผ ์ด์ง์์๋ฅผ ๊ฐ์ ธ์ค์์ค.
select name, count(*) from tblInsa; -- ์๋ฌ
-- 2. ORA-00934: group function is not allowed here
-- where์ ์๋ ์ง๊ณ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
-- ์ง๊ณ ํจ์ ๋ฐํ๊ฐ(์งํฉ), ์ปฌ๋ผ(๊ฐ์ธ)
-- where์ ์ ๊ฐ์ธ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฌํ๋ ์์ญ > ์งํฉ๊ฐ์ ๋ํ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค.
-- ์๊ตฌ์ฌํญ] ํ๊ท ๊ธ์ฌ๋ณด๋ค ๋ ๋ง์ด ๋ฐ๋ ์ง์์ ๊ฐ์ ธ์ค์์ค.
select avg(basicpay) from tblInsa; --155๋ง์
select * from tblInsa where basicpay >= avg(basicpay); --์๋ฌ
1. count()
- ๊ฒฐ๊ณผ ํ
์ด๋ธ์ ๋ ์ฝ๋ ์๋ฅผ ๋ฐํํ๋ค.
- number count(์ปฌ๋ผ๋ช
)
- null ๋ ์ฝ๋ ์ ์ธ(*****)
select name from tblCountry;
select count(name) from tblCountry;
-- 'AS'์ ์ํ ๋๋ผ ๊ฐฏ์?
select count(name) from tblCountry where continent = 'AS';
select count(population) from tblCountry; --null์ ํฌํจ๋์ง ์์
select count(*) from tblCountry; --null๊ณผ ์๊ด์์ด ํ
์ด๋ธ์ ํ ๊ฐฏ์ ์ ํํ๊ฒ ํ์
-- ๋ชจ๋ ์ง์์?
-- ์ฐ๋ฝ์ฒ๊ฐ ์๋ ์ง์์?
-- ์ฐ๋ฝ์ฒ๊ฐ ์๋ ์ง์์?
select count(*) from tblInsa; -- 60
select count(tel) from tblInsa; -- 57
select count(*) - count(tel) from tblInsa; -- 3
select count(*) from tblInsa where tel is not null; --57
select count(*) from tblInsa where tel is null; --3
-- tblInsa. ์ด๋ค ๋ถ์๋ค ์๋์?
select distinct buseo from tblInsa;
-- tblInsa. ๋ถ์๊ฐ ๋ช๊ฐ ์๋์?
select count(distinct buseo) from tblInsa; --7๊ฐ
--tblComedian. ๋จ์์? ์ฌ์์?
select * from tblComedian;
select count(*) from tblComedian where gender = 'm'; --8
select count(*) from tblComedian where gender = 'f'; --2
-- *** ์์ฃผ ์ฌ์ฉ๋๋ ํจํด
select
count(*) as ์ ์ฒด์ธ์์,
count(case
when gender = 'm' then 1 --1 ๋ง๊ณ ์๋ฌด๊ฑฐ๋ ๋ฃ์ด๋ ์๊ดX
end) as ๋จ์์ธ์์,
count(case
when gender = 'f' then 1
end) as ์ฌ์์ธ์์
from tblComedian;
--tblInsa. ๊ธฐํ๋ถ ๋ช๋ช
? ์ด๋ฌด๋ถ ๋ช๋ฉ? ๊ฐ๋ฐ๋ถ ๋ช๋ช
?
select count(*) from tblInsa where buseo = '๊ธฐํ๋ถ'; --7
select count(*) from tblInsa where buseo = '์ด๋ฌด๋ถ'; --7
select count(*) from tblInsa where buseo = '๊ฐ๋ฐ๋ถ'; --14
select
count(case
when buseo = '๊ธฐํ๋ถ' then 1
end) as ๊ธฐํ๋ถ,
count(case
when buseo = '์ด๋ฌด๋ถ' then 1
end) as ์ด๋ฌด๋ถ,
count(case
when buseo = '๊ฐ๋ฐ๋ถ' then 1
end) as ๊ฐ๋ฐ๋ถ
from tblInsa;
-- count ์์๋ ๋จ์ผ์ปฌ๋ผ or *๋ง ๊ฐ๋ฅ
select count(name, buseo) from tblInsa; -- count์์ ๋๊ฐ ์ด์์ ์ปฌ๋ผX
2. sum()
- ํด๋น ์ปฌ๋ผ์ ํฉ์ ๊ตฌํ๋ค.
- number sum(์ปฌ๋ฌ๋ช
)
- ์ซ์ํ๋ง ์ ์ฉ ๊ฐ๋ฅ
select sum(height), sum(weight) from tblComedian;
select sum(first) from tblComedian; --๋ฌธ์์ด > ์๋ฌ(ORA-01722: invalid number)
select
sum(basicpay) as "์ง์ถ ๊ธ์ฌ ํฉ",
sum(sudang) as "์ง์ถ ์๋น ํฉ",
sum(basicpay) + sum(sudang) as "์ด ์ง์ถ",
sum(basicpay + sudang) as "์ด ์ง์ถ"
from tblInsa;
3. avg()
- ํด๋น ์ปฌ๋ผ์ ํ๊ท ๊ฐ์ ๊ตฌํ๋ค.
- number avg(์ปฌ๋ผ๋ช
)
- ์ซ์ํ๋ง ์ ์ฉ ๊ฐ๋ฅ
- null์ ์ ์ธ๋จ(*****)
-- tblInsa. ํ๊ท ๊ธ์ฌ?
select sum(basicpay) / 60 from tblInsa; --1556526
select sum(basicpay) / count(*) from tblInsa; --1556526
select avg(basicpay) from tblInsa; --1556526
-- ํ๊ท ์ธ๊ตฌ์?
select
avg(population), --15588
sum(population) / count(*), --14475
sum(population) / count(population) --15588
from tblCountry;
-- ํ์ฌ > ์ฑ๊ณผ๊ธ ์ง๊ธ > ์ฑ๊ณผ๊ธ ์ถ์ฒ > 1ํ ๊ณต๋ก~
-- 1. ๊ท ๋ฑ ์ง๊ธ: ์ด์ง๊ธ์ก / ๋ชจ๋ ์ง์์ = sum() / count(*)
-- 2. ์ฐจ๋ฑ ์ง๊ธ: ์ด์ง๊ธ์ก / ์ฐธ์ฌ ์ง์์ = sum() / count(์ฐธ์ฌ์ธ์์) = avg()
4. max()
- objec max(์ปฌ๋ผ๋ช
)
- ์ต๋๊ฐ ๋ฐํ
5. min()
- objec max(์ปฌ๋ผ๋ช
)
- ์ต์๊ฐ ๋ฐํ
- ์ซ์ํ, ๋ฌธ์ํ, ๋ ์งํ ๋ชจ๋ ์ ์ฉ
select max(height), min(height) from tblComedian; --์ซ์ํ
select max(name), min(name) from tblInsa; --๋ฌธ์ํ
select max(ibsadate), min(ibsadate) from tblInsa; --๋ ์งํ
select
count(*) as "์ง์์",
sum(basicpay) as "์ด๊ธ์ฌ ํฉ",
avg(basicpay) as "ํ๊ท ๊ธ์ฌ",
max(basicpay) as "์ต๊ณ ๊ธ์ฌ",
min(basicpay) as "์ต์ ๊ธ์ฌ"
from tblInsa;
ex09_numerical_function
์ซ์ ํจ์(= ์ํ ํจ์)
round()
- ๋ฐ์ฌ๋ฆผ ํจ์
- number round(์ปฌ๋ผ๋ช
) : ์ ์ ๋ฐํ
- number round(์ปฌ๋ผ๋ช
, ์์์ดํ ์๋ฆฟ์) : ์ค์ ๋ฐํ
select
height / weight,
round(height / weight),
round(height / weight, 1),
round(height / weight, 2),
round(height / weight, 3),
round(height / weight, 0)
from tblComedian;
-- ํ๊ท ๊ธ์ฌ
select round(avg(basicpay)) from tblInsa; --1556526.666666666666666666666666666666667
floor(), trunc()
- ์ ์ญ ํจ์(๋ฐ๋ฅ)
- ๋ฌด์กฐ๊ฑด ๋ด๋ฆผ ํจ์
- number floor(์ปฌ๋ผ๋ช
) : ๋ฌด์กฐ๊ฑด ์ ์ ๋ฐํ
- number trunc(์ปฌ๋ผ๋ช
[, ์์์ดํ ์๋ฆฟ์]) : ์ ์ or ์ค์ ๋ฐํ
select
height / weight,
round(height / weight),
floor(height / weight),
trunc(height / weight),
trunc(height / weight, 1),
trunc(height / weight, 2)
from tblComedian;
ceil()
- ๋ฌด์กฐ๊ฑด ์ฌ๋ฆผ ํจ์(์ฒ์ฅ)
- number ceil(์ปฌ๋ผ๋ช
)
select
height / weight,
round(height / weight),
floor(height / weight),
ceil(height / weight)
from tblComedian;
mod()
- ๋๋จธ์ง ํจ์
- number mod(ํผ์ ์, ์ ์)
- dual ํ ์ด๋ธ > ๋ ์ฝ๋ 1๊ฐ์ง๋ฆฌ ํ ์ด๋ธ
select sysdate from dual;
select
10 / 3,
mod(10, 3) as "๋๋จธ์ง", -- ์๋ฐ ์ ์%์ ์
floor(10 / 3) as "๋ชซ" -- ์๋ฐ ์ ์/์ ์
from dual;
abs() : ์ ๋๊ฐ
power() : ์ ๊ณฑ
sqrt() : ์ ๊ณฑ๊ทผ
select
abs(10), -- abs() : ์ ๋๊ฐ
abs(-10),
power(2, 2), -- power() : ์ ๊ณฑ
power(2, 3),
power(2, 4),
sqrt(4), -- sqrt() : ์ ๊ณฑ๊ทผ
sqrt(9),
sqrt(16)
from dual;