집계 함수명 ( DISTINCT 칼럼이나 표현식 ) |
---|
SELECT COUNT(*) "전체 행수"
, COUNT(HEIGHT) "키 건수"
, MAX(HEIGHT) "최대키"
, MIN(HEIGHT) "최소키"
, ROUND(AVG(HEIGHT),2) "평균키"
FROM PLAYER;
SELECT [DISTINCT] 칼럼명 [ALIAS명]
FROM 테이블명
[WHERE 조건식 ]
[GROUP BY 칼럼(Column)이나 표현식]
[HAVING 그룹조건식];
SELECT POSITION 포지션
, COUNT(*) 인원수
, COUNT(HEIGHT) 키대상
, MAX(HEIGHT) 최대키
, MIN(HEIGHT) 최소키
, ROUND(AVG(HEIGHT),2) 평균키
FROM PLAYER
SELECT POSITION 포지션
, COUNT(*) 인원수
, COUNT(HEIGHT) 키대상
, MAX(HEIGHT) 최대키
, MIN(HEIGHT) 최소키
, ROUND(AVG(HEIGHT),2) 평균키
FROM PLAYER
GROUP BY POSITION;
SELECT POSITION 포지션
, ROUND(AVG(HEIGHT),2) 평균키
FROM PLAYER
WHERE AVG(HEIGHT) >= 180
GROUP BY POSITION;
* - - * 3행에 오류: ERROR: 집계 함수는 허가되지 않는다.*
SELECT POSITION 포지션
, ROUND(AVG(HEIGHT),2) 평균키
FROM PLAYER
GROUP BY POSITION;
HAVING AVG(HEIGHT) >= 180
SELECT ENAME, DEPTNO, EXTRACT(MONTH FROM HIREDATE) 입사월, SAL FROM EMP; -- ORACLE
SELECT ENAME, DEPTNO, DATEPART(MONTH, HIREDATE) 입사월, SAL FROM EMP; --SQL SERVER
SELECT ENAME, DEPTNO, MONTH(HIREDATE) 입사월, SAL FROM EMP; --SQL SERVER
SELECT ENAME
, DEPTNO
, CASE MONTH WHEN 1 THEN SAL END M01
, CASE MONTH WHEN 2 THEN SAL END M02
, CASE MONTH WHEN 3 THEN SAL END M03
, CASE MONTH WHEN 4 THEN SAL END M04
, CASE MONTH WHEN 5 THEN SAL END M05
, CASE MONTH WHEN 6 THEN SAL END M06
, CASE MONTH WHEN 7 THEN SAL END M07
, CASE MONTH WHEN 8 THEN SAL END M08
, CASE MONTH WHEN 9 THEN SAL END M09
, CASE MONTH WHEN 10 THEN SAL END M10
, CASE MONTH WHEN 11 THEN SAL END M11
, CASE MONTH WHEN 12 THEN SAL END M12
FROM
( SELECT ENAME
, DEPTNO
, EXTRACT(MONTH FROM HIREDATE) MONTH
, SAL
FROM EMP
)
;
SELECT DEPTNO,
AVG(CASE MONTH WHEN 1 THEN SAL END) M01,
AVG(CASE MONTH WHEN 2 THEN SAL END) M02,
AVG(CASE MONTH WHEN 3 THEN SAL END) M03,
AVG(CASE MONTH WHEN 4 THEN SAL END) M04,
AVG(CASE MONTH WHEN 5 THEN SAL END) M05,
AVG(CASE MONTH WHEN 6 THEN SAL END) M06,
AVG(CASE MONTH WHEN 7 THEN SAL END) M07,
AVG(CASE MONTH WHEN 8 THEN SAL END) M08,
AVG(CASE MONTH WHEN 9 THEN SAL END) M09,
AVG(CASE MONTH WHEN 10 THEN SAL END) M10,
AVG(CASE MONTH WHEN 11 THEN SAL END) M11,
AVG(CASE MONTH WHEN 12 THEN SAL END) M12
FROM
(
SELECT ENAME
, DEPTNO
, EXTRACT(MONTH FROM HIREDATE) MONTH
, SAL
FROM EMP
)
GROUP BY DEPTNO
-- DECODE
SELECT DEPTNO,
AVG(DECODE( MONTH , 1 , SAL )) M01,
AVG(DECODE( MONTH , 2 , SAL )) M02,
AVG(DECODE( MONTH , 3 , SAL )) M03,
AVG(DECODE( MONTH , 4 , SAL )) M04,
AVG(DECODE( MONTH , 5 , SAL )) M05,
AVG(DECODE( MONTH , 6 , SAL )) M06,
AVG(DECODE( MONTH , 7 , SAL )) M07,
AVG(DECODE( MONTH , 8 , SAL )) M08,
AVG(DECODE( MONTH , 9 , SAL )) M09,
AVG(DECODE( MONTH , 10 , SAL )) M10,
AVG(DECODE( MONTH , 11 , SAL )) M11,
AVG(DECODE( MONTH , 12 , SAL )) M12
FROM
(
SELECT DEPTNO
, EXTRACT(MONTH FROM HIREDATE) MONTH
, SAL
FROM EMP
)
GROUP BY DEPTNO
SELECT TEAM_ID
, NVL(SUM(CASE POSITION WHEN 'FW' THEN 1 ELSE 0 END),0) FW -- 불필요한 연산이 일어남
-- SUM은 NULL을 제외 하고 합산 하므로 SUM안이 아닌 밖에 NVL를 사용 하여 건수들 만큼 자원의 낭비를 줄임
, NVL(SUM(CASE POSITION WHEN 'MF' THEN 1 END),0) MF
-- SQL SERVER로 변경 하고자 할 경우 ISNULL(SUM(CASE POSITION WHEN 'DF' THEN 1 END),0) DF
, NVL(SUM(CASE POSITION WHEN 'DF' THEN 1 END),0) DF
-- SEARCHED_CASE_EXPRESSION 표현 NVL(SUM(CASE POSITION = 'GK' THEN 1 END),0) GK
, NVL(SUM(CASE POSITION WHEN 'GK' THEN 1 END),0) GK
, COUNT(*) SUM
FROM PLAYER
GROUP BY TEAM_ID;