好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Oracle 计算时间格式平均值的sql 语句

Oracle 计算时间格式平均值的sql 语句

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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 0 + 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完事,这个时间下面要做对比:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

--获取年

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 语句,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/hong-dan/archive/2019/08/27/11418219.html

查看更多关于Oracle 计算时间格式平均值的sql 语句的详细内容...

  阅读:37次