【问题标题】:Sql Date to Hours (total)Sql 日期到小时(总计)
【发布时间】:2019-06-13 13:30:41
【问题描述】:

我有这个问题:

   SELECT
     NUMTODSINTERVAL(
     SUM( TO_DATE( MT.TI_CONTR, 'HH24:MI' ) - TO_DATE( '00:00', 'HH24:MI' ) ),
     'DAY'
     ) AS total
   FROM MYTABLE MT;

执行此查询我得到以下信息:

+22 19:02:00.000000
+94 19:26:00.000000
+46 03:50:00.000000
+76 08:30:00.000000
+44 02:42:00.000000

这当然是以天为单位分组,一旦达到 24 小时。

TI_CONTR 列是一个 varchar,以这种格式存储小时和分钟:hh:mm(例如 '05:22')。

我怎样才能得到总小时数的结果(前 252:20)?

谢谢

【问题讨论】:

    标签: sql oracle date sum timestamp


    【解决方案1】:

    Oracle 不允许您对 INTERVAL 数据类型执行 SUM(),因此最好使用老式的 SUBSTR() 和数学来解决这个问题。

    with dat as (SELECT '19:02' t1_contr from dual 
                 union all
                 SELECT '19:26' t1_contr from dual 
                 union all
                 SELECT '03:50' t1_contr from dual 
                 union all
                 SELECT '08:30' t1_contr from dual 
                 union all
                 SELECT '02:42' t1_contr from dual 
                 )
    select to_char(sum(substr(t1_contr,1,2)) --sum the hours, then add 
                 + trunc(sum(substr(t1_contr,4,2))/60)) --the hours portion of the summed minutes
                 ||':'|| -- put in your separator
                 to_char( mod(sum(substr(t1_contr,4,2)),60)) --and append the summed minutes after removing the hours
    from dat   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-25
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多