请问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 ? 请指点!!!

解决方案 »

  1.   

    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 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)
      

  2.   


    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)
      

  3.   

    这个其实很简单的。就是先把相差的时间按秒求出来,最后转换下就OK了。
    转换公式也很通用的,如果你经常使用的话可以做成函数。select trunc(&second/3600) as hours,trunc(mod(&second,3600)/60) as minutes,mod(&second,60) as seconds from dual;
      

  4.   

    被逼无奈,没有好的办法,只能用最笨的方法 直接转换。select FLOOR(sum((TO_DATE(END_TIME, 'yyyy-mm-dd hh24:mi:ss') -
                     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