-- TOP 1~5 만 구현했어요 OUTER JOIN으로 JOIN 하시면 될겁니다. -- TOP 이 같다면 ORDER BY 조건을 추가하세요. -- 손으로 적는거라 오타 있을수 있어요 SELECT BASE_DATE , USR_NO , MAX(CASE WHEN RN =1 THEN MENU_NM END ) TOP1 , MAX(CASE WHEN RN =2 THEN MENU_NM END ) TOP2 , MAX(CASE WHEN RN =3 THEN MENU_NM END ) TOP3 , MAX(CASE WHEN RN =4 THEN MENU_NM END ) TOP4 , MAX(CASE WHEN RN =5 THEN MENU_NM END ) TOP5 FROM ( SELECT BASE_DATE , USR_NO , MENU_NM , ROW_NUMBER() OVER(PARTITION BY BASE_DATE , USR_NO ORDER BY MENU_CNT DESC ) RN FROM ( SELECT DISTINCT TO_DATE(날짜,'YYYYMMDD' ) BASE_DATE , USER_NO , MENU_NM , COUNT(*) OVER(PARTITION BY TO_CHAR(날짜,'YYYYMMDD')) MENU_CNT FROM A_TABLE ) ) GROUP BY BASE_DATE , USR_NO
SELECT ymd , usr_no , MIN(CASE rn WHEN 1 THEN menu_nm END) top1 , MIN(CASE rn WHEN 2 THEN menu_nm END) top2 , MIN(CASE rn WHEN 3 THEN menu_nm END) top3 , MIN(CASE rn WHEN 4 THEN menu_nm END) top4 , MIN(CASE rn WHEN 5 THEN menu_nm END) top5 , IFNULL(SUM(cnt1), 0) cnt_00_06 , IFNULL(SUM(cnt2), 0) cnt_06_09 , IFNULL(SUM(cnt3), 0) cnt_09_13 , IFNULL(SUM(cnt4), 0) cnt_13_16 , IFNULL(SUM(cnt5), 0) cnt_16_19 , IFNULL(SUM(cnt6), 0) cnt_19_21 , IFNULL(SUM(cnt7), 0) cnt_21_24 FROM (SELECT ymd , usr_no , menu_nm , ROW_NUMBER() OVER(PARTITION BY ymd, usr_no ORDER BY COUNT(*) DESC, menu_nm) rn , COUNT(CASE WHEN hh >= '00' AND hh < '06' THEN 1 END) cnt1 , COUNT(CASE WHEN hh >= '06' AND hh < '09' THEN 1 END) cnt2 , COUNT(CASE WHEN hh >= '09' AND hh < '13' THEN 1 END) cnt3 , COUNT(CASE WHEN hh >= '13' AND hh < '16' THEN 1 END) cnt4 , COUNT(CASE WHEN hh >= '16' AND hh < '19' THEN 1 END) cnt5 , COUNT(CASE WHEN hh >= '19' AND hh < '21' THEN 1 END) cnt6 , COUNT(CASE WHEN hh >= '21' AND hh < '24' THEN 1 END) cnt7 FROM (SELECT usr_no , menu_nm . SUBSTR(log_tktm, 1, 8) ymd , SUBSTR(log_tktm, 9, 2) hh FROM t WHERE menu_nm NOR IN ('login', 'logout') ) a GROUP BY ymd, usr_no, menu_nm ) a GROUP BY ymd, usr_no ;