Oracle 計(jì)算時(shí)間格式平均值的sql 語(yǔ)句
select
to_char((to_date('2019-07-01', 'yyyy-mm-dd') + numtodsinterval(avg(begin_time_second), 'second')),'hh24:mi:ss') avg_begin_time,
to_char((to_date('2019-07-01', 'yyyy-mm-dd') + numtodsinterval(avg(end_time_second), 'second')),'hh24:mi:ss') avg_end_time
--2019-07-01 00:00:00 + numtodsinterval(平均數(shù),'秒')轉(zhuǎn)換為日期格式,然后再轉(zhuǎn)換為 時(shí)間字符格式
from
(
select
--把上班時(shí)間換算為秒
to_char(a.actontime, 'hh24') * 3600 +
to_char(a.actontime, 'mi') * 60 +
to_char(a.actontime, 'ss') as begin_time_second,
--把下班時(shí)間換算為秒
to_char(a.actofftime, 'hh24') * 3600 +
to_char(a.actofftime, 'mi') * 60 +
to_char(a.actofftime, 'ss') as end_time_second
from empworkdate a
)
知識(shí)點(diǎn)擴(kuò)展:oracle中獲取年月日時(shí)分秒
我寫sql的時(shí)間為2016年10月10日下午15:18完事,這個(gè)時(shí)間下面要做對(duì)比:
--獲取年
select to_char(sysdate,'yyyy') from dual--2016
select to_char(sysdate,'YYYY') from dual--2016
--獲取月
select to_char(sysdate,'mm') from dual--10
select to_char(sysdate,'MM') from dual--10
--獲取日
select to_char(sysdate,'dd') from dual--10
select to_char(sysdate,'DD') from dual--10
--獲取時(shí)
select to_char(sysdate,'hh24') from dual--15
select to_char(sysdate,'hh') from dual--03
select to_char(sysdate,'HH') from dual--03
select to_char(sysdate,'HH24') from dual--15
--獲取分鐘
select to_char(sysdate,'MI') from dual--14
select to_char(sysdate,'mi') from dual--14
--獲取秒
select to_char(sysdate,'ss') from dual--35
select to_char(sysdate,'SS') from dual--40
--獲取年月日時(shí)分秒
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual--2016-10-10 03:17:25
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual--2016-10-10 15:17:51
(1)從上面的測(cè)試SQL可以看出年月日yyyy,mm,dd,hh,mi,ss的大小寫對(duì)于從oracle中獲取年月日時(shí)分秒沒有影響
(2)對(duì)于獲取小時(shí)分12小時(shí)和24小時(shí)的情況,如果你想獲取顯示下午的時(shí)間,你可以采用hh24,這樣就會(huì)顯示類似于15:30而不是03:30
總結(jié)
以上所述是小編給大家介紹的Oracle 計(jì)算時(shí)間格式平均值的sql 語(yǔ)句,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
您可能感興趣的文章:- Oracle數(shù)據(jù)庫(kù)按時(shí)間進(jìn)行分組統(tǒng)計(jì)數(shù)據(jù)的方法
- oracle中得到一條SQL語(yǔ)句的執(zhí)行時(shí)間的兩種方式
- oracle日期時(shí)間型timestamp的深入理解
- ORACLE時(shí)間函數(shù)(SYSDATE)深入理解