-- 중복행의 제거
SELECT DISTINCT JOB_ID
FROM EMPLOYEES;
-- GROUP BY
-- 같은 그룹으로 묶어줌
SELECT JOB_ID
FROM EMPLOYEES
GROUP BY JOB_ID;
SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
ORDER BY DEPARTMENT_ID;
-- HAVING(GROUP BY절과 같이 사용, 단독으로는 사용불가)
-- GROUP BY의 조건절
SELECT DEPARTMENT_ID,SUM(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;
SELECT JOB_ID,SUM(SALARY)
FROM EMPLOYEES
GROUP BY JOB_ID
HAVING SUM(SALARY) >= 100000;
-- 그룹 여러개 가능
SELECT DEPARTMENT_ID,JOB_ID,COUNT(SALARY), SUM(SALARY), SUM(SALARY)/COUNT(SALARY), AVG(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID,JOB_ID;
/*
그룹함수와 연계해서 사능하는 것
COUNT(*) OVER(PARTITION BY)
MAX
MIN
SUM
RANK()
ROW_NUMBER()
DENSE_RANK()
*/
SELECT FIRST_NAME,JOB_ID,SALARY,
COUNT(JOB_ID) OVER(PARTITION BY JOB_ID),
SUM(SALARY)OVER(PARTITION BY JOB_ID)
FROM EMPLOYEES;
-- 그룹함수
/*
COUNT
SUM
AVG
MAX
MIN
*/
-- 그룹함수 (SALARY로 묶음)
SELECT COUNT(SALARY)
FROM EMPLOYEES
WHERE JOB_ID = 'IT_PROG';
-- 갯수 , 합계 , 평균
SELECT COUNT(SALARY), SUM(SALARY), SUM(SALARY)/COUNT(SALARY), AVG(SALARY)
FROM EMPLOYEES
WHERE JOB_ID = 'IT_PROG';
/*
NVL(대상이 되는 컬럼, 0)
대상이 되는 컬럼 != NULL -> 대상이 되는 컬럼
대상이 되는 컬럼 == NULL -> 0
NVL2(대상이 되는 컬럼, 1 ,2)
대상이 되는 컬럼 != NULL -> 1
대상이 되는 컬럼 == NULL -> 2
*/
SELECT EMPLOYEE_ID, MANAGER_ID,DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90;
SELECT EMPLOYEE_ID, NVL(MANAGER_ID,0),DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90;
SELECT EMPLOYEE_ID, NVL2(MANAGER_ID,'있음','없음'),DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90;
'Oracle Database > SQL' 카테고리의 다른 글
SQL 쿼리문 JOIN (0) | 2018.06.26 |
---|---|
SQL 쿼리문 기본 (0) | 2018.06.26 |
SQL 쿼리 정렬 (0) | 2018.06.26 |
SQL명령어 (0) | 2018.06.26 |
Oracle Database 사용방법 및 주석 (0) | 2018.06.20 |