데이터의 통계자료를 조회할 때, 시간대별, 일별, 주별, 월별 데이터를 조회하고자 한다.
아래의 SQL문을 응용하여 통계자료를 조회할 때 사용하자.
-- 시간대별 통계
SELECT HOUR(CRE_DTM) AS 'HOUR'
, COUNT(OS_TYPE) AS IOS
FROM TB_API_ACCESS_LOG
WHERE cre_dtm between '2013-08-04 11:00:00' and '2013-08-05 12:00:00' -- 시간 범위 조건
AND OS_TYPE = 'OS_00003'
GROUP BY HOUR(CRE_DTM)
ORDER BY HOUR(CRE_DTM) DESC
-- 일별 통계
select DAYOFMONTH(cre_dtm) AS 'DAY'
, COUNT(OS_TYPE) AS IOS
from tb_api_access_log
where cre_dtm between '2013-08-04 11:00:00' and '2013-08-05 12:00:00' -- 시간 범위 조건
group by DAYOFMONTH(cre_dtm) -- 일별 그룹
ORDER BY DAYOFMONTH(cre_dtm) DESC
-- 요일별 통계
select CASE WHEN DAYOFWEEK(cre_dtm) = 1 THEN '일'
WHEN DAYOFWEEK(cre_dtm) = 2 THEN '월'
WHEN DAYOFWEEK(cre_dtm) = 3 THEN '화'
WHEN DAYOFWEEK(cre_dtm) = 4 THEN '수'
WHEN DAYOFWEEK(cre_dtm) = 5 THEN '목'
WHEN DAYOFWEEK(cre_dtm) = 6 THEN '금'
WHEN DAYOFWEEK(cre_dtm) = 7 THEN '토'
ELSE '오류' END WEEK_NAME
, count(row_num) from tb_api_access_log
where cre_dtm between '2013-08-01 11:00:00' and '2013-08-06 12:00:00' -- 시간 범위 조건
group by DAYOFWEEK(cre_dtm) -- 요일별 그룹
-- 주별 통계
select WEEK(cre_dtm), count(row_num) from tb_api_access_log
where cre_dtm between '2013-08-01 11:00:00' and '2013-08-05 12:00:00' -- 시간 범위 조건
group by WEEK(cre_dtm) -- 주별 그룹(1년중 몇번째 주인지 표시)
-- 월별 통계
select MONTH(cre_dtm), count(row_num) from tb_api_access_log
where cre_dtm between '2013-07-29 11:00:00' and '2013-08-05 12:00:00' -- 시간 범위 조건
group by MONTH(cre_dtm) -- 월별 그룹
댓글 없음:
댓글 쓰기