[Oracle] ๐Ÿ’พ ๊ทธ๋ฃน ํ•จ์ˆ˜ (count, sum, avg, max, min, group by)

๊ทธ๋ฃน ํ•จ์ˆ˜


ํ•˜๋‚˜ ์ด์ƒ์˜ ํ–‰์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ์—ฐ์‚ฐํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ฃน ํ•จ์ˆ˜๋Š” null์„ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐํ•œ๋‹ค.


๊ทธ๋ฃน ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜


  • count : ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    select count(comm) from emp;
    -- commition์„ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ธ์› ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    select count(*) from emp;
    -- ์ „์ฒด ์‚ฌ์›์˜ ์ธ์› ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • sum : ์ปฌ๋Ÿผ ๊ฐ’๋“ค์˜ ์ด ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.

    select sum(sal) from emp;
    -- ์ „์ฒด ์‚ฌ์›์˜ ๊ธ‰์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.
  • avg : ์ปฌ๋Ÿผ ๊ฐ’๋“ค์˜ ํ‰๊ท  ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

    select avg(sal) from emp; 
    -- ์ „์ฒด ์‚ฌ์›์˜ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค.
  • max : ์ปฌ๋Ÿผ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

    select max(sal) from emp; 
    -- ์ „์ฒด ์‚ฌ์›์˜ ๊ธ‰์—ฌ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ๊ธ‰์—ฌ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
  • min : ์ปฌ๋Ÿผ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

    select min(sal) from emp;
    -- ์ „์ฒด ์‚ฌ์›์˜ ๊ธ‰์—ฌ ์ค‘ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ธ‰์—ฌ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
  • group by
    ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ง‘๊ณ„๋ฅผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ถ€์„œ๋ณ„ ์ธ์› ์ˆ˜, ํ•™๊ธ‰๋ณ„ ์„ฑ์  ๋“ฑ๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. group by ๋’ค์— ๊ธฐ์ค€์ด ๋˜๋Š” ์ปฌ๋Ÿผ์„ ์ ๋Š”๋‹ค.

    ```
    select deptno, count(*) from emp group by deptno;
    -- ๋ถ€์„œ๋ณ„ ์ธ์› ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
    ```

    ๊ทธ๋ฃน ํ•จ์ˆ˜๋ฅผ ์“ธ ๋•Œ๋Š” select ๋’ค์— group by ๋’ค์— ์˜ค๋Š” ์ปฌ๋Ÿผ๋งŒ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

    select empno, count(*) from emp group by deptno;
    -- ์ด๋ ‡๊ฒŒ๋Š” ์“ธ ์ˆ˜ ์—†๋‹ค. (์˜ค๋ฅ˜)

having


group by๋กœ ์ง‘๊ณ„๋œ ๊ฐ’์— where์ ˆ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. where์ ˆ์ด ์œ ํšจํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” where์ ˆ์ด group by์ ˆ๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋˜์–ด, where์ ˆ์˜ ์กฐ๊ฑด์ด group by์ ˆ์— ์ ์šฉ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿ“ sql select ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ์ˆœ์„œ
from - on - join - where - group by - cube | rollup - having - select - distinct - order by - top

having์ ˆ์€ select ๊ธฐ์ค€ ์ปฌ๋Ÿผ, ์—ฐ์‚ฐ ๊ฐ’ from ํ…Œ์ด๋ธ” groub by ๊ธฐ์ค€ ์ปฌ๋Ÿผ having ์กฐ๊ฑด์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

-- ์ธ์›์ด 5๋ช… ์ด์ƒ์ธ ๋ถ€์„œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ๋ถ€์„œ๋ณ„ ํ‰๊ท  ๊ธ‰์—ฌ
select deptno, round(avg(sal), 0) from emp group by deptno having count(*) >= 5;

group by ์—ฐ์Šต ๋ฌธ์ œ


๐Ÿ” ๋ฌธ์ œ

  1. ๋ถ€์„œ ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ช…, ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
  2. 'DALLAS'์—์„œ ๊ทผ๋ฌดํ•˜๋Š” ์‚ฌ์›๋“ค์˜ job๋ณ„ ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
  3. ๊ธ‰์—ฌ ๋“ฑ๊ธ‰(grade)๋ณ„ ์ธ์› ์ˆ˜๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. (์ธ์› ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ)
  4. job๋ณ„ ๊ธ‰์—ฌ ๋“ฑ๊ธ‰์„ ๊ตฌํ•˜์‹œ์˜ค. (ํ‰๊ท  ๊ธ‰์—ฌ ๋“ฑ๊ธ‰ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ)
  5. ๋ถ€์„œ ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ช…, ํ‰๊ท  ๊ธ‰์—ฌ, ํ‰๊ท  ๊ธ‰์—ฌ ๋“ฑ๊ธ‰์„ ๊ตฌํ•˜์‹œ์˜ค.

์—ฐ์Šต์— ์‚ฌ์šฉํ•œ emp ํ…Œ์ด๋ธ”, dept ํ…Œ์ด๋ธ”, salgrade ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (Oracle DB scott ๊ณ„์ •์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”)

๐Ÿ’ก ๋‹ต์•ˆ

  1. ๋‚ด๋ถ€ ์กฐ์ธ์„ ํ–ˆ์„ ๋•Œ 40๋ฒˆ ๋ถ€์„œ๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•œ๋‹ค.
select d.deptno, d.dname, nvl(round(avg(sal)), 0) as avg
from emp e right outer join dept d on e.deptno=d.deptno group by d.deptno, d.dname;
select e.job, round(avg(e.sal)) as avg
from emp e inner join dept d on e.deptno=d.deptno
where d.loc='DALLAS' group by e.job;
select s.grade, count(*) as count
from emp e inner join salgrade s on e.sal between s.losal and s.hisal group by s.grade
order by 2 desc;
select e.job, round(avg(s.grade), 1) as avg
from emp e inner join salgrade s on e.sal between s.losal and s.hisal group by e.job
order by 2 asc; 
  1. 1๋ฒˆ ๋ฌธ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 40๋ฒˆ ๋ถ€์„œ๊ฐ€ ์ถœ๋ ฅ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” emp ํ…Œ์ด๋ธ”๊ณผ salgrade ํ…Œ์ด๋ธ”์„ ๋จผ์ € ๋‚ด๋ถ€ ์กฐ์ธํ•œ ๊ฒฐ๊ณผ์— dept ํ…Œ์ด๋ธ”์„ ์™ธ๋ถ€ ์กฐ์ธํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด 40๋ฒˆ ๋ถ€์„œ๋Š” ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.
select d.deptno, d.dname, nvl(round(avg(e.sal), 0), 0) as sal, nvl(round(avg(s.grade), 1), 0) as grade
from emp e inner join salgrade s on e.sal between s.losal and s.hisal
right outer join dept d on e.deptno=d.deptno
group by d.deptno, d.dname;

๐Ÿ“ ๊ฐ„๋‹จ ๋ฌธ๋ฒ• ์„ค๋ช…

round(์ปฌ๋Ÿผ, ๋ฐ˜์˜ฌ๋ฆผํ•  ์ž๋ฆฌ ๊ฐ’)
์‰ฝ๊ฒŒ ๋งํ•ด ์†Œ์ˆ˜์  ๋ช‡ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜์˜ฌ๋ฆผํ•  ์ž๋ฆฌ ๊ฐ’์€ ์†Œ์ˆ˜์ ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ(์˜ค๋ฅธ์ชฝ์œผ๋กœ) 0, 1, 2...๋กœ ๋งค๊ธด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0์„ ์ ์œผ๋ฉด ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†Œ์ˆ˜์  ์—†์ด ์ •์ˆ˜๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. 1์„ ์ ์œผ๋ฉด ์†Œ์ˆ˜์  ์•„๋ž˜ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅ๋œ๋‹ค.

nvl(์ปฌ๋Ÿผ, null์ผ ๊ฒฝ์šฐ์˜ ๋ฐ˜ํ™˜ ๊ฐ’)
์˜ค๋ผํด์—์„œ๋งŒ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. null ๊ฐ’์„ ์ง€์ •ํ•ด ๋†“์€ ๊ฐ’์œผ๋กœ ์น˜ํ™˜ํ•ด ์ค€๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ