'ㅁ'초등학교 6학년의 2학기기말성적 데이터를 가지고 반(class)최고득점자와 학년전체 최고득점자를 출력하는 문제입니다.
- 2학기기말 성적데이터
with t as
(
select '1' class,'a' name, 80 kor, 75 eng, 80 math from dual union all
select '1' class,'b' name, 90 kor, 80 eng, 85 math from dual union all
select '1' class,'c' name, 80 kor, 95 eng, 70 math from dual union all
select '2' class,'d' name, 70 kor, 85 eng, 75 math from dual union all
select '2' class,'e' name, 95 kor, 80 eng, 90 math from dual union all
select '2' class,'f' name, 60 kor, 70 eng, 70 math from dual
)
select *
from t
CLASS |
NAME |
KOR |
ENG |
MATH |
1 |
a |
80 |
75 |
80 |
1 |
b |
90 |
80 |
85 |
1 |
c |
80 |
95 |
70 |
2 |
d |
70 |
85 |
75 |
2 |
e |
95 |
80 |
90 |
2 |
f |
60 |
70 |
70 |
- 결과데이터
CLASS |
NAME |
KOR |
ENG |
MATH |
1 |
a |
80 |
75 |
80 |
1 |
b |
90 |
80 |
85 |
1 |
c |
80 |
95 |
70 |
1 |
반최고득점자 |
b |
c |
b |
2 |
d |
70 |
85 |
75 |
2 |
e |
95 |
80 |
90 |
2 |
f |
60 |
70 |
70 |
2 |
반최고득점자 |
e |
d |
e |
|
최고득점자 |
e |
c |
e |
반최고득점자 : 각 과목별로 class내 최고득점자name
최고득점자 : 각 과목별로 학년내 최고득점자name
<<
select
class,
case grouping(name)
when 0 then name
else case grouping(class)
when 0 then '반최고득점자'
else '최고득점자'
end
end name,
decode(grouping(name),1,max(name) keep (dense_rank last order by kor),
max(kor)) kor,
decode(grouping(name),
1,max(name) keep (dense_rank last order by eng),
max(eng)) eng,
decode(grouping(name),
1,max(name) keep (dense_rank last order by math),
max(math)) math
from t
group by grouping sets
(
(class,name),
(class),
()
)