ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/Oracle

[ex08~09] ์ง‘๊ณ„ํ•จ์ˆ˜(aggregation function), ์ˆ˜ํ•™ ํ•จ์ˆ˜(numerical_function)

๋Œ€์žฅ์ฟต์•ผ 2023. 3. 19. 15:48

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;