请问oracle 如何计算出时间差的 小时 分钟 秒 比如:表里有两列
2013-01-11 09:03:20 2013-01-11 10:08:29 相差1小时5分钟9秒
2013-01-12 09:03:20 2013-01-14 10:04:21 相差48小时
2013-01-15 15:24:02 2013-01-15 17:20:57 相差1小时56分55秒
统计出来的是: 51:02:04 ? 请指点!!!
2013-01-11 09:03:20 2013-01-11 10:08:29 相差1小时5分钟9秒
2013-01-12 09:03:20 2013-01-14 10:04:21 相差48小时
2013-01-15 15:24:02 2013-01-15 17:20:57 相差1小时56分55秒
统计出来的是: 51:02:04 ? 请指点!!!
解决方案 »
- 关于Oracle中向IBM MQ发送消息的问题?
- 急求一个SQL语句的写法,有关半全角字符转化
- .NET 调用oracle 10G出错
- ASM下的oracle10G遭遇突然断电,起不来了
- 日志文件损坏,system表空间存在一致性问题,怎么办啊!!!
- 一个有趣的Sql
- 保存一篇几千字的文章(有HTML标示)用什么字段?如何保证保存和读出不是乱码呢?
- 每读取100000条记录写一次文件需要怎么写SQL
- 各位师哥帮我看一下怎么样才能提高效率!
- 怎么写sql语句可以把数据库中一个字段中相同的只显示一条出来,列出所有不同的记录?
- with as语句性能求优化
- oracle 服务器和客户端装一台机器,sqlplus报协议适配器错误
select '2013-01-11 09:03:20' as st,'2013-01-11 10:08:29' as et from dual
union all
select '2013-01-12 09:03:20' as st,'2013-01-14 10:04:21' as et from dual
union all
select '2013-01-15 15:24:02' as st,'2013-01-15 17:20:57' as et from dual
)
SELECT TRUNC(sum(CT) / 3600, 0) || ':' || TRUNC(MOD(sum(CT), 3600) / 60, 0) || ':' || MOD(sum(CT), 60) AS TT
FROM (select (to_char(to_date(et, 'yyyy-mm-dd hh24:mi:ss'), 'dd') * 24 * 60 * 60 +
to_char(to_date(et, 'yyyy-mm-dd hh24:mi:ss'), 'hh24') * 60 * 60 +
to_char(to_date(et, 'yyyy-mm-dd hh24:mi:ss'), 'mi') * 60 +
to_char(to_date(et, 'yyyy-mm-dd hh24:mi:ss'), 'ss')) -
(to_char(to_date(st, 'yyyy-mm-dd hh24:mi:ss'), 'dd') * 24 * 60 * 60 +
to_char(to_date(st, 'yyyy-mm-dd hh24:mi:ss'), 'hh24') * 60 * 60 +
to_char(to_date(st, 'yyyy-mm-dd hh24:mi:ss'), 'mi') * 60 +
to_char(to_date(st, 'yyyy-mm-dd hh24:mi:ss'), 'ss')) as CT
from test)
with test as (
select '2013-01-11 09:03:20' as st,'2013-01-11 10:08:29' as et from dual
union all
select '2013-01-12 09:03:20' as st,'2013-01-14 10:04:21' as et from dual
union all
select '2013-01-15 15:24:02' as st,'2013-01-15 17:20:57' as et from dual
)
select trim(to_char(trunc(dif_time / 3600), '09')) || ':' ||
trim(to_char(trunc(mod(dif_time, 3600) / 60), '09')) || ':' ||
trim(to_char(mod(dif_time, 60), '09')) as sum_dif_time
from (select sum((to_date(et, 'yyyy-mm-dd hh24:mi:ss') -
to_date(st, 'yyyy-mm-dd hh24:mi:ss')) * 24 * 60 * 60) as dif_time
from test)
转换公式也很通用的,如果你经常使用的话可以做成函数。select trunc(&second/3600) as hours,trunc(mod(&second,3600)/60) as minutes,mod(&second,60) as seconds from dual;
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24) HOUR,
FLOOR((sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24 -
FLOOR(sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24)) * 60) MINUTE,
FLOOR((((sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24 -
FLOOR(sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24)) * 60 -
FLOOR((sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME, 'yyyy-mm-dd hh24:mi:ss'))) * 24 -
FLOOR(sum((TO_DATE(END_TIME,
'yyyy-mm-dd hh24:mi:ss') -
TO_DATE(BEGIN_TIME,
'yyyy-mm-dd hh24:mi:ss'))) * 24)) * 60)) * 60)) second
from DUAL