[퀴즈] 그룹별 최고점수, 전체건수, 최고점수의 건수 구하기 1 10 7,901

by 마농 group by keep [2010.01.25 09:20:06]


[퀴즈] 그룹별로 최고점수, 전체건수 및 최고점수 보유자의 건수를 구하고
       최고점수를 많이 보유하고 있는 그룹 순서대로 정렬하시오.

 

WITH t AS
(
SELECT 1 grp, 99 jumsu FROM dual
UNION ALL SELECT 1, 99 FROM dual
UNION ALL SELECT 1, 98 FROM dual
UNION ALL SELECT 1, 97 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 86 FROM dual
UNION ALL SELECT 3, 76 FROM dual
UNION ALL SELECT 3, 69 FROM dual
UNION ALL SELECT 3, 59 FROM dual
)
SELECT * FROM t;

 

[원본]     [결과]      

GRP

JUMSU

 

GRP

MAX_JUMSU

TOT_CNT

MAX_CNT

1 99   2 87 5 4
1 99   1 99 4 2
1 98   3 76 3 1
1 97          
2 87          
2 87          
2 87          
2 87          
2 86          
3 76          
3 69          
3 59          

 

정답은 몇일뒤에 올리겠습니다.

[정답보기] <=== 트리플클릭
SELECT grp
     , MAX(jumsu) jumsu
     , COUNT(*) tot_cnt
     , COUNT(*) KEEP(DENSE_RANK LAST ORDER BY jumsu) max_cnt
  FROM t
 GROUP BY grp
 ORDER BY max_cnt DESC
;

by 호야 [2010.01.25 10:16:48]
우아...풀었는데.... 역시나 어렵다...ㅠ.ㅠ 모두 화이팅

by 쭌 [2010.01.25 10:52:36]
SELECT grp,
max(jumsu),
count(*) CNT,
count(*) KEEP(DENSE_RANK FIRST ORDER BY JUMSU desc) F_CNT
FROM t
GROUP BY grp

by 마농 [2010.01.26 13:45:12]
잘 플어주셨네요. 정렬 안하신것 빼고요..
정답 올렸습니다. 확인해 보세요.

by pranludi [2010.01.28 20:01:45]
SELECT grp, max(jumsu) as max_jumsu, count(grp) as cnt_grp
, count(*) KEEP(DENSE_RANK FIRST ORDER BY jumsu desc) max_cnt
FROM t
group by grp
order by max_cnt desc
;; keep 에서 삽질...

by Ejql [2010.01.29 00:41:55]
keep이란것이 있군요?

by 김종만 [2010.02.02 11:09:40]
저도 keep 처음 알았네요. 유용하네요. max() keep(), min() keep(), count() keep().

by f [2010.03.11 18:07:25]
WITH t AS
(
SELECT 1 grp, 99 jumsu FROM dual
UNION ALL SELECT 1, 99 FROM dual
UNION ALL SELECT 1, 98 FROM dual
UNION ALL SELECT 1, 97 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 87 FROM dual
UNION ALL SELECT 2, 86 FROM dual
UNION ALL SELECT 3, 76 FROM dual
UNION ALL SELECT 3, 69 FROM dual
UNION ALL SELECT 3, 59 FROM dual
)
SELECT T2.GRP, T2.TOT_CNT, T2.MAX_JUMSU, T1.MAX_CNT
FROM (SELECT GRP, JUMSU, COUNT(GRP) AS MAX_CNT FROM T GROUP BY GRP, JUMSU ) T1,
(SELECT GRP, COUNT(GRP)AS TOT_CNT, MAX(JUMSU) AS MAX_JUMSU FROM T GROUP BY GRP) T2
WHERE T1.GRP = T2.GRP
AND T1.JUMSU = T2.MAX_JUMSU
ORDER BY MAX_CNT DESC

젠장 공부 열심히 하겠습니다;;;

by 티지다 [2012.01.03 15:12:02]
select distinct grp, max_jumsu, tot_cnt , count(jumsu)over(partition by grp) max_cnt
from(
SELECT distinct max(jumsu)over(partition by grp) max_jumsu ,
count(jumsu)over(partition by grp) tot_cnt
FROM t
) a ,t
where max_Jumsu = jumsu

by 손님 [2012.12.25 01:39:44]

SELECT grp, MAX(jumsu) MAX_JUMSU, count(jumsu) TOT_CNT, COUNT(DECODE(rk,'1',1)) MAX_CNT
FROM(
  SELECT grp,jumsu, rank() OVER(PARTITION BY grp ORDER BY jumsu DESC) rk
  FROM t
)
GROUP BY grp

by 야쿠르트세컨드 [2012.12.25 01:42:23]
SELECT grp, MAX(jumsu) MAX_JUMSU, count(jumsu) TOT_CNT, COUNT(DECODE(rk,'1',1)) MAX_CNT
FROM(
  SELECT grp,jumsu, rank() OVER(PARTITION BY grp ORDER BY jumsu DESC) rk
  FROM t
)
GROUP BY grp

위에꺼 제가 올렸습니다; 로그인안하고 올렸네요;
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입