먼저 아래와 같은 소요 시간이 들어 있는 테이블이 있습니다.
WITH TT AS( SELECT '01:00:00' T FROM DUAL UNION ALL SELECT '02:00:00' T FROM DUAL UNION ALL SELECT '03:00:00' T FROM DUAL UNION ALL SELECT '04:00:00' T FROM DUAL ) SELECT T FROM TT ;
이 소요 시간의 평균을 구하고 싶은데요..
그냥 단순 하게 생각해서 아래와 같이 만들었습니다.
WITH TT AS( SELECT ROUND(AVG(TO_NUMBER(REGEXP_SUBSTR(T,'[^:]+',1,1))*60*60 + TO_NUMBER(REGEXP_SUBSTR(T,'[^:]+',1,2))*60 + TO_NUMBER(REGEXP_SUBSTR(T,'[^:]+',1,3)))) T_SEC FROM ( SELECT '01:00:00' T FROM DUAL UNION ALL SELECT '02:00:00' T FROM DUAL UNION ALL SELECT '03:00:00' T FROM DUAL UNION ALL SELECT '04:00:00' T FROM DUAL ) ) SELECT T_SEC , TO_CHAR(TRUNC(T_SEC/60/60),'00') ||':'|| TRIM(TO_CHAR(TRUNC((T_SEC - TRUNC(T_SEC/60/60)*60*60)/60),'00')) ||':'|| TRIM(TO_CHAR(T_SEC - TRUNC(T_SEC/60/60)*60*60 - TRUNC((T_SEC - TRUNC(T_SEC/60/60)*60*60)/60)*60,'00')) AVG_SEC FROM TT ;
혹시 더 좋은 방법이 있을까요?
이곳 저곳 찾아 봤는데..
시간을 뺄수는 있는데.. 더하는건 문제가 있어서...
자문을 구해 봅니다.
감사합니다.
WITH TT AS( SELECT '01:00:00' T FROM DUAL UNION ALL SELECT '02:00:00' T FROM DUAL UNION ALL SELECT '03:00:00' T FROM DUAL UNION ALL SELECT '04:00:00' T FROM DUAL ) SELECT AVG ( TO_CHAR ( TO_DATE ( t, 'HH24:MI:SS' ), 'SSSSS' ) ) , TO_CHAR ( TO_DATE ( AVG ( TO_CHAR ( TO_DATE ( t, 'HH24:MI:SS' ), 'SSSSS' ) ), 'SSSSS' ), 'HH24:MI:SS' ) FROM tt
WITH TT AS(
SELECT '01:00:00' T FROM DUAL UNION ALL
SELECT '02:00:00' T FROM DUAL UNION ALL
SELECT '03:00:00' T FROM DUAL UNION ALL
SELECT '04:00:00' T FROM DUAL
)
SELECT t
, TO_CHAR ( TO_DATE ( t, 'SSSSS' ), 'HH24:MI:SS' )
FROM (
SELECT AVG ( TO_CHAR ( TO_DATE ( t, 'HH24:MI:SS' ), 'SSSSS' ) ) t
FROM tt
)