Sql日期到小时数(总计)
我有一个疑问:Sql日期到小时数(总计),sql,oracle,date,sum,timestamp,Sql,Oracle,Date,Sum,Timestamp,我有一个疑问: 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
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”)
如何获得总小时数的结果(ex 252:20)
感谢Oracle不允许您对间隔数据类型执行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
Oracle不允许您对区间数据类型执行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