SELECT ORDER_DT,
COUNT(*),
RANK() OVER(ORDER BY COUNT(*) DESC) AS RANK
FROM STARBUCKS_ORDER
GROUP BY ORDER_DT;
SELECT ORDER_DT,
COUNT(*),
DENSE_RANK() OVER(ORDER BY COUNT(*) DESC) AS DENSE_RANK
FROM STARBUCKS_ORDER
GROUP BY ORDER_DT;
SELECT ORDER_DT,
COUNT(*),
ROW_NUMBER() OVER(ORDER BY COUNT(*) DESC) AS ROW_NUMBER
FROM STARBUCKS_ORDER
GROUP BY ORDER_DT;
//↓모든 사람들의 점수의 합계 구하는 쿼리
SELECT SUM(SCORE) AS TOTAL_SCORE FROM SQLD;
//↓개인별 총점수를 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
SUM(SCORE) OVER(PARTITION BY STUDENT_NAME) AS TOTAL_SCORE
FROM SQLD;
//↓모든 점수를 대상으로 최댓값을 반환
SELECT MAX(SCORE) AS MAX_SCORE FROM SQLD;
//↓과목별 최대 점수를 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
MAX(SCORE) OVER(PARTITION BY SUBJECT) AS MAX_SCORE
FROM SQLD;
//↓모든 점수를 대상으로 최솟값 반환
SELECT MIN(SCORE) AS MIN_SCORE FROM SQLD;
//↓과목별 최소 점수를 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
MIN(SCORE) OVER(PARTITION BY SUBJECT) AS MIN_SCORE
FROM SQLD;
//↓모든 점수를 대상으로 평균값 반환
SELECT AVG(SCORE) AS AVG_SCORE FROM SQLD;
//↓과목별 평균 점수를 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
ROUND(AVG(SCORE) OVER(PARTITION BY SUBJECT)) AS AVG_SCORE
FROM SQLD;
//↓모든 점수를 대상으로 건수를 반환
SELECT COUNT(*) AS MAX_SCORE FROM SQLD;
//↓과목별 PASS한 건수를 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
COUNT(*) OVER(PARTITION BY SUBJECT) AS PASS_COUNT
FROM SQLD
WHERE RESULT = 'PASS';
WINDOWING절을 이용하여 집계하려는 데이터의 범위를 지정할 수 있음.
예시)
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
: 처음부터 현재 행까지, RANGE UNBOUNDED PRECEDING과 같은 의미
RANGE BETWEEN 10 PRECEDING AND CURRENT ROW
: 현재 행이 가지고 있는 값볻 100만큼 적은 행부터 현재 행까지, RANGE 10 PRECEDING 과 같은 의미
RANGE GETWEEN CURRENT ROW AND UNBOUDED FOLLOWING
: 현재 행부터 끝까지
ROWS BETWEEN CURRENT ROW AND 5 FOLLOWING
: 현재 행부터 아래로 5만큼 이동한 행까지
// ↓ 모든 점수를 대상으로 오름차순 후 첫번째에 위치하는 점수를 출력.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
FIRST_VALUE(SCORE) OVER(PARTITION BY SUBJECT
ORDER BY SCORE DESC) AS FIRST_VALUE
FROM SQLD;
// ↓ 모든 점수를 대상으로 오름차순 후 마지막에 위치하는 점수를 출력.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
LAST_VALUE(SCORE) OVER(PARTITION BY SUBJECT
ORDER BY SCORE DESC) AS LAST_VALUE
FROM SQLD;
// 이와 같이 사용하면 원하는대로 출력이 안된다.
// WINDOWING절의 default가 RANGE UNBOUNDED PRECEDING이어서 파티션의 범위가 맨 위 끝 행부터 현재행까지로 지정되었기 때문에 원하는대로 출력이 안되는 것.
// 아래와 같이 WINDOWING절을 명시해줘야함.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
LASST_VALUE(SCORE) OVER(PARTITION BY SUBJECT
ORDER BY SCORE DESC
RANGE BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) AS LAST_VALUE
FROM SQLD;
//과목별로 본인보다 2만큼 앞에 있는(높은) 점수를 구하는 쿼리.
//LAG의 두번째 인자(여기서는 2)를 생략하면 default 1이 됨.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
LAG(SCORE,2) OVER(PARTITION BY SUBJECT
ORDER BY SCORE DESC) AS LAG
FROM SQLD;
//과목별로 본인보다 2만큼 뒤에 있는(낮은) 점수를 구하는 쿼리.
//LEAD의 두번째 인자(여기서는 2)를 생략하면 default 1이 됨.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
LEAD(SCORE, 2) OVER(ORDER BY SCORE) AS LEAD
FROM SQLD;
// 과목별 SCORE 합계에서 차지하는 비율을 구하는 쿼리.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
SUM(SCORE) OVER(PRTITION BY SUBJECT) AS SUM,
SCORE/SUM(SCORE) OVER (PARTITION BY SUBJECT) AS "SCORE/SUM",
RATIO_TO_REPORT(SCORE) OVER(PARTITION BY SUBJECT) AS RATIO_TO_REPORT
FROM SQLD;
// PERCENT_RANK 함수의 결과가 RANK순위 값에서 1을 뺀 값을 총 COUNT에서 1을 뺀 값으로 나눈 값과 동일함.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
RANK() OVER(ORDER BY SCORE) AS RANK,
COUNT(*) OVER() AS COUNT,
(RANK() OVER (ORDER BY SCORE) -1) /(COUNT(*) OVER()-1) AS "(RANK-1)/(COUNT-1)",
PERCENT_RANK() OVER(ORDER BY SCORE) AS PERCENT_RANK
FROM SQLD;
// 과목별로 나눈 파티션에서 해당 SCORE가 차지하는 백분위 순위를 구하는 쿼리.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
RANK() OVER(PARTITION BY SUBJECT ORDER BY SCORE) AS RANK,
COUNT(*) OVER(PARTITION BY SUBJECT) AS COUNT,
(RANK() OVER (PARTITION BY SUBJECT ORDER BY SCORE) -1) /(COUNT(*) OVER()-1) AS "(RANK-1)/(COUNT-1)",
PERCENT_RANK() OVER(PARTITION BY SUBJECT ORDER BY SCORE) AS PERCENT_RANK
FROM SQLD;
// CUME_DIST 함수의 결과가 현재 행까지의 누적 건수에서 전체 건수로 나눈 값과 동일함.
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
COUNT(*) OVER(ORDER BY SCORE) AS COUNT,
COUNT(*) OVER() AS TOTAL_COUNT,
COUNT(*) OVER(ORDER BY SCORE)/COUNT(*) OVER() AS "COUNT/TOTAL_COUNT",
CUME_DIST() OVER(ORDER BY SCORE) AS CUME_DIST
FROM SQLD;
// 과목별로 나눈 파티션에서 해당 SCORE에 해당하는 누적 백분율을 구하는 쿼리
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
COUNT(*) OVER(PARTITION BY SUBJECT ORDER BY SCORE) AS COUNT,
COUNT(*) OVER(PARTITION BY SUBJECT ) AS TOTAL_COUNT,
COUNT(*) OVER(PARTITION BY SUBJECT ORDER BY SCORE)/COUNT(*) OVER() AS "COUNT/TOTAL_COUNT",
CUME_DIST() OVER(PARTITION BY SUBJECT ORDER BY SCORE) AS CUME_DIST
FROM SQLD;
SELECT STUDENT_NAME,
SUBJECT,
SCORE,
NTILE(1) OVER(ORDER BY SCORE DESC) AS NTILE1,
NTILE(3) OVER(ORDER BY SCORE DESC) AS NTILE3,
NTILE(5) OVER(ORDER BY SCORE DESC) AS NTILE5,
FROM SQLD;
SELECT ROWNUM,
이름,
국어,
수학,
영어
FROM EXAM_SCORE;
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY 국어 DESC, 영어 DESC, 수학 DESC) AS RNUM,
이름,
국어,
영어,
수학
FROM EXAM_SCORE)
WHERE RNUM <=5;
SELECT * FROM (
SELECT RANK() OVER(ORDER BY 국어 DESC, 영어 DESC, 수학 DESC) AS RANK,
이름,
국어,
영어,
수학
FROM EXAM_SCORE)
WHERE RANK <=5;
SELECT * FROM (
SELECT DENSE_RANK() OVER(ORDER BY 국어 DESC, 영어 DESC, 수학 DESC) AS DR,
이름,
국어,
영어,
수학
FROM EXAM_SCORE)
WHERE DR <=5;
SELECT A.CATEGORY_TYPE,
A.CATEGORY_NAME,
B.CATEGORY_TYPE,
B.CATEGORY_NAME,
C.CATEGORY_TYPE,
C.CATEGORY_NAME
FROM CATEGORY A,
CATEGORY B,
CATEGORY C
WHERE A.CATEGORY_NAME = B.PARENT_CATEGORY
AND B.CATEGORY_NAME = C.PARENT_CATEGORY;
// 위의 셀프조인의 쿼리를 계층 쿼리로 변경하면 아래와 같다.
SELECT LEVEL,
SYS_CONNECT_BY_PATH('['||CATEGORY_TYPE||']'||CATEGORY_NAME,'-') AS PATH
FROM CATEGORY
START WITH PARENT_CATEGORY IS NULL,
CONNECT BY PRIOR CATEGORY_NAME = PARENT_CATEGORY;