簡體   English   中英

以HH:MM格式添加時間-SQL查詢

[英]Adding Time in HH:MM Format -SQL Query

先生/馬姆,

我正在使用Oracle 11g。下表為“ Timeduration”。 它包含以小時和分鍾為單位指定的時間。我想獲取以小時和分鍾為單位的總時間。

Timeduration
05:37
06:40
03:45
02:50
07:58

我希望總時間為25:30。 我如何為此編寫代碼? 請幫幫我。”

with t as (
  select '05:37' as timeduration from dual union all
  select '06:40' as timeduration from dual union all
  select '03:45' as timeduration from dual union all
  select '02:50' as timeduration from dual union all
  select '07:58' as timeduration from dual
),
d as (
  select 
      --
      --  4  Converting the sum obtained in (3) into
      --     an interval
    numtodsinterval(
      -- 
      --  3  Summing the day fractions obtained in (2)
      sum(
      --
      --  1  Convert the string 'HH:MM' to a real date.
      --
      --     For example 05:37 becomes January 1st 0001 5:37
             to_date('00010101' || t.timeduration, 'yyyymmddhh24:mi') 
      --
      --  2  Subtract January 1st 0001 from that date
      --
      --     The result is a number of a day's length
      --    [1: 24 hours, 0.3: 8 hours etc)
           - date '0001-01-01'),
      --
          'DAY'
    ) interval
  from
    t
)
select
   (
      --  5  How many days are in the interval. Each day
      --     corresponds to 24 hours:
      extract (day  from d.interval) * 24 +
      --
      --  6  The hours «below» 24 hours must be added
      --     seperatly:
      extract (hour from d.interval) 
   ) 
      --
      --  7  Hours are now extracted. Append the delimiter
     || ':' ||
   (
      --
      --  8  Finally: get the minutes from the interval
      extract (minute from d.interval)
   )
   result
from d;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM