상품명 | 판매수 |
A | 10 |
B | 20 |
C | 20 |
D | 30 |
E | 10 |
F | 10 |
G | 10 |
H | 50 |
I | 10 |
J | 10 |
K | 10 |
L | 10 |
M | 10 |
L | 10 |
O | 2 |
P | 3 |
Q | 4 |
R | 5 |
S | 6 |
T | 7 |
U | 8 |
V | 9 |
W | 1 |
X | 2 |
Y | 3 |
Z | 4 |
순위 | 상품명 | 판매수 | 판매율 |
1 | H | 50 | 25% |
2 | D | 30 | 15% |
3 | B | 20 | 6% |
4 | C | 20 | 6% |
5 | A | 10 | 3% |
6 | 기타 | 80 | 48% |
210 | 100% |
WITH t AS ( SELECT 'A' cd, 10 cnt FROM dual UNION ALL SELECT 'B', 20 FROM dual UNION ALL SELECT 'C', 20 FROM dual UNION ALL SELECT 'D', 30 FROM dual UNION ALL SELECT 'E', 10 FROM dual UNION ALL SELECT 'F', 10 FROM dual UNION ALL SELECT 'G', 10 FROM dual UNION ALL SELECT 'H', 50 FROM dual UNION ALL SELECT 'I', 10 FROM dual UNION ALL SELECT 'J', 10 FROM dual UNION ALL SELECT 'K', 10 FROM dual UNION ALL SELECT 'L', 10 FROM dual UNION ALL SELECT 'M', 10 FROM dual UNION ALL SELECT 'L', 10 FROM dual UNION ALL SELECT 'O', 2 FROM dual UNION ALL SELECT 'P', 3 FROM dual UNION ALL SELECT 'Q', 4 FROM dual UNION ALL SELECT 'R', 5 FROM dual UNION ALL SELECT 'S', 6 FROM dual UNION ALL SELECT 'T', 7 FROM dual UNION ALL SELECT 'U', 8 FROM dual UNION ALL SELECT 'V', 9 FROM dual UNION ALL SELECT 'W', 1 FROM dual UNION ALL SELECT 'X', 2 FROM dual UNION ALL SELECT 'Y', 3 FROM dual UNION ALL SELECT 'Z', 4 FROM dual ) SELECT rk , DECODE(rk, 6, '기타', cd) cd , SUM(cnt) cnt , ROUND(RATIO_TO_REPORT(SUM(cnt)) OVER(PARTITION BY GROUPING(rk)) * 100, 2) rat FROM (SELECT cd, cnt , LEAST(6, RANK() OVER(ORDER BY cnt DESC, cd)) rk FROM t ) GROUP BY ROLLUP((rk, DECODE(rk, 6, '기타', cd))) ; -- 추가질문에 대한 답변 -- -- 비율을 구한뒤 합산하면 +- 오차가 발생될수밖에 없지요. -- 합산한걸 가지고 비율을 구한다면 무조건 100% 지요. -- 굳이 개별항목에 대한 오차보정을 할 필요는 없을 듯.