1. 程式人生 > 資料庫 >Oracle 計算時間格式平均值的sql 語句

Oracle 計算時間格式平均值的sql 語句

Oracle 計算時間格式平均值的sql 語句

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),'hh24:mi:ss') avg_end_time
--2019-07-01 00:00:00 + numtodsinterval(平均數,'秒')轉換為日期格式,然後再轉換為 時間字元格式
from 
(
  select 
  --把上班時間換算為秒
  to_char(a.actontime,'hh24') * 3600 +
  to_char(a.actontime,'mi') * 60 +
  to_char(a.actontime,'ss') as begin_time_second,  --把下班時間換算為秒 
  to_char(a.actofftime,'hh24') * 3600 +
  to_char(a.actofftime,'mi') * 60 + 
  to_char(a.actofftime,'ss') as end_time_second
  from empworkdate a
)

知識點擴充套件:oracle中獲取年月日時分秒

我寫sql的時間為2016年10月10日下午15:18完事,這個時間下面要做對比:

--獲取年
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
--獲取時
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
--獲取年月日時分秒
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)從上面的測試SQL可以看出年月日yyyy,mm,dd,hh,mi,ss的大小寫對於從oracle中獲取年月日時分秒沒有影響

(2)對於獲取小時分12小時和24小時的情況,如果你想獲取顯示下午的時間,你可以採用hh24,這樣就會顯示類似於15:30而不是03:30

總結

以上所述是小編給大家介紹的Oracle 計算時間格式平均值的sql 語句,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!