1) count() : 입력되는 데이터의 총 건수를 반환.
count(*) : null 값을 포함한 결과
count(comm) : null 값을 제외한 결과
예시 : 교수 테이블에서 교수전체의 인원수와 교수중 보너스 받는 교수의 인원수 조회하기.
count(bonus) : bonus 컬럼의 값이 null이 아닌 레코드 갯수
select count(*) 전체인원수, count(bonus) "보너스를 받는 인원수",
count(*)-count(bonus) "보너스 없는 인원수" from professor;
select * from professor where bonus is null;
문제 :
1. 학생의 전체인원수와 지도교수가 배정된 인원수, 지도교수가 없는 인원수 조회하기.
select count(*) 학생수, count(profno) "배정된 인원수",
count(*)-count(profno) " 배정 안된 인원수" from student;
2) sum() : 입력된 데이터들의 합계값을 구하는 함수.
예시 :교수들에게 지급되는 전체 급여 합계 출력하기.
select sum(pay) from professor;
문제 :
1. 교수들에게 지급되는 부서별(deptno) 전체 급여 합계 출력하기.
급여합계가 많은 순으로 정렬.
select deptno, sum(pay) from professor group by deptno order by 2 desc;
select deptno, sum(pay) from professor group by deptno order by sum(Pay) desc;
3) avg() : 입력된 값들의 평균값을 구해주는 함수.
null값은 자동제외시킨다.(결과 도출을 위해서는 따로 지정해줘야한다.)
문제 :
1. 교수들에게 지급되는 부서별(deptno) 전체 급여 합계, 부서별 급여평균 출력하기.
급여평균이 많은 순으로 정렬.
select deptno, sum(pay) 합계, avg(pay) 평균 from professor
group by deptno order by 3 desc;
select deptno, sum(pay) 합계, avg(pay) 평균 from professor
group by deptno order by avg(pay) desc;
문제 :
2. 교수의 부서별 인원수, 급여평균, 보너스 평균 조회하기
select deptno, count(*), avg(pay), avg(bonus) from professor group by deptno;
-- null값 포함 안됨.
select name, bonus from professor where deptno = 103;
-- 데이터가 null 값이면 평균의 대상에서 제외됨.
select deptno, count(*), avg(pay), avg(nvl(bonus,0)) from professor group by deptno;
-- null값을 포함시키는 경우.
모든 그룹함수는 null값 제외함. 평균산출에서도 null값은 제외됨.
: 100, null50, 50 => 200/3 => 평균값으로 계산됨.
200/4 => 평균값에서도 null 포함되는 경우.
'수업(국비지원) > Oracle' 카테고리의 다른 글
| [Oracle] SQL 복수행 함수(그룹함수) - having, group by (0) | 2023.04.14 |
|---|---|
| [Oracle] SQL 복수행 함수(그룹함수) - max(), min(), stddev(), variance() (0) | 2023.04.14 |
| [Oracle] SQL 단일행 함수 - 일반 함수 nvl(), decode(), case 구문 (0) | 2023.04.14 |
| [Oracle] SQL 단일행 함수 - 형 변환 함수 to_char(), to_number, to_date (0) | 2023.04.14 |
| [Oracle] SQL 단일행 함수 - 날짜 관련 함수 sysdate, months_between, add_months, next_day, last_day (0) | 2023.04.14 |