找出两个日期之间的差异,以小时和分钟为单位

时间:2016-02-17 10:43:36

标签: sql oracle date-difference

我试图想出一种方法来计算两小时和小时之间的差异。

我的表格有两列Start DateTimeStamp

Start Date              Timestamp
--------------------    --------------------
05/JAN/2016 05:30:00    01/JAN/2016 10:02:29
30/JAN/2016 06:10:00    18/JAN/2016 19:24:00
23/JAN/2016 06:10:00    08/JAN/2016 10:46:00
05/JAN/2016 05:30:00    30/DEC/2015 16:07:00
23/JAN/2016 06:10:00    08/JAN/2016 12:18:05
01/JAN/2016 14:10:00    16/DEC/2015 16:36:56
01/JAN/2016 14:10:00    16/DEC/2015 11:41:00
03/JAN/2016 05:15:00    02/JAN/2016 11:23:15
03/JAN/2016 05:15:00    02/JAN/2016 07:52:00

我使用查询:

select ROUND(RM_LIVE.CRWGNDACTTIME.GNDACTSTARTRM_LIVE.TRANSACTIONLOG.TIMESTAMP,2)
                       AS "Difference"
  from Transaction;

查询结果为:

0.002721428571428571428571428571428571428571
0.008178571428571428571428571428571428571429
0.0105785714285714285714285714285714285714
0.003971428571428571428571428571428571428571

预期结果:

133:23
 91:28
355:24
353:52
274:46

我使用以下公式在Excel中得到了预期的结果:

= MAX(T982+U982,W982+V982) - MIN(T982+U982,W982+V982)

如何在Oracle SQL中获得相同的结果?

CASE
  WHEN trunc(24 * abs(RM_LIVE.TRANSACTIONLOG.TIMESTAMP
- RM_LIVE.CRWGNDACTTIME.GNDACTSTART))
  ||':'|| lpad(round(60 * mod(24 * abs(RM_LIVE.TRANSACTIONLOG.TIMESTAMP
- RM_LIVE.CRWGNDACTTIME.GNDACTSTART), 1)), 2, '0')  <= '11:00' THEN     'LESS'
ELSE 'MORE'
END AS "mORE/LESS",


386:29  1055    01-JAN-16   16-DEC-15   MORE
**102:41    1055    08-NOV-15   04-NOV-15   LESS**
381:33  1055    01-JAN-16   16-DEC-15   MORE
176:45  1055    20-NOV-15   12-NOV-15   MORE
**119:54    1055    08-NOV-15   03-NOV-15   LESS** 

1 个答案:

答案 0 :(得分:2)

我在this answer中展示了几个与解释相关的变体,但它似乎比你想要的稍微多一些 - 你不想看到秒 - 并且不会&#39 ; t允许超过100小时。

获得所需输出的最简单方法是:

trunc(24 * (RM_LIVE.CRWGNDACTTIME.GNDACTSTART
    - RM_LIVE.TRANSACTIONLOG.TIMESTAMP))
  ||':'|| lpad(round(60 * mod(24 * (RM_LIVE.CRWGNDACTTIME.GNDACTSTART
    - RM_LIVE.TRANSACTIONLOG.TIMESTAMP), 1)), 2, '0')
as difference

第一部分获得整个小时数,这类似于您在评论中添加的方法,但是截断而不是舍入到仅获得整个小时。然后是一个冒号分隔符。然后通过从小时计算得到剩余部分来计算分钟数 - 通过mod() - 这是小时数的小数,然后乘以60. lpad()将前导零加到分钟数上,但你可以改用to_char()

如果你有时间戳可以在开始时间之前或之后的范围混合,那么你可以使用the abs() function来获得肯定的结果。

trunc(24 * abs(RM_LIVE.CRWGNDACTTIME.GNDACTSTART
    - RM_LIVE.TRANSACTIONLOG.TIMESTAMP))
  ||':'|| lpad(round(60 * mod(24 * abs(RM_LIVE.CRWGNDACTTIME.GNDACTSTART
    - RM_LIVE.TRANSACTIONLOG.TIMESTAMP), 1)), 2, '0')
as difference

作为一个演示,您的数据在一个表格中被模拟:

create table your_table(id, start_time, timestamp) as
  select 1, to_date ('05/JAN/2016 05:30:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('01/JAN/2016 10:02:29', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 2, to_date ('30/JAN/2016 06:10:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('18/JAN/2016 19:24:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 3, to_date ('23/JAN/2016 06:10:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('08/JAN/2016 10:46:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 4, to_date ('05/JAN/2016 05:30:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('30/DEC/2015 16:07:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 5, to_date ('23/JAN/2016 06:10:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('08/JAN/2016 12:18:05', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 6, to_date ('01/JAN/2016 14:10:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('16/DEC/2015 16:36:56', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 7, to_date ('01/JAN/2016 14:10:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('16/DEC/2015 11:41:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 8, to_date ('03/JAN/2016 05:15:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('02/JAN/2016 11:23:15', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 9, to_date ('03/JAN/2016 05:15:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('02/JAN/2016 07:52:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 10, to_date ('16/JAN/2016 11:15:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('16/JAN/2016 12:44:00', 'DD/MON/YYYY HH24:MI:SS') from dual
  union all select 11, to_date ('16/JAN/2016 11:15:00', 'DD/MON/YYYY HH24:MI:SS'), to_date('16/JAN/2016 12:50:00', 'DD/MON/YYYY HH24:MI:SS') from dual;

等效查询:

select start_time, timestamp, trunc(24 * abs(start_time - timestamp))
    ||':'|| lpad(round(60 * mod(24 * abs(start_time - timestamp), 1)), 2, '0')
    as difference
from your_table
order by id;

START_TIME          TIMESTAMP           DIFFERENCE
------------------- ------------------- ----------
2016-01-05 05:30:00 2016-01-01 10:02:29 91:28     
2016-01-30 06:10:00 2016-01-18 19:24:00 274:46    
2016-01-23 06:10:00 2016-01-08 10:46:00 355:24    
2016-01-05 05:30:00 2015-12-30 16:07:00 133:23    
2016-01-23 06:10:00 2016-01-08 12:18:05 353:52    
2016-01-01 14:10:00 2015-12-16 16:36:56 381:33    
2016-01-01 14:10:00 2015-12-16 11:41:00 386:29    
2016-01-03 05:15:00 2016-01-02 11:23:15 17:52     
2016-01-03 05:15:00 2016-01-02 07:52:00 21:23     
2016-01-16 11:15:00 2016-01-16 12:44:00 1:29      
2016-01-16 11:15:00 2016-01-16 12:50:00 1:35      

你不能轻易地比较你想要的字符串值 - 它必须是一个像91:28这样的值的字符串 - 还有别的东西,因为数字的字符串比较并不好用。如你所见,比较&#119; 54:54&#39;与&#39; 11:00&#39;是有效地比较每个字符串的第三个字符,因为前两个字符串是相同的,所以9:

将其作为比较的小数部分更简单:

CASE
  WHEN round(24 * abs(RM_LIVE.TRANSACTIONLOG.TIMESTAMP
    - RM_LIVE.CRWGNDACTTIME.GNDACTSTART), 2) <= 11 THEN 'LESS"
ELSE 'MORE'
END AS "mORE/LESS",

对于91:28的例子,它将比较小数部分版本91.46;对于119:54将比较119.9,超过11; 102:41将被比较为102.68,也超过11。

或者您可以通过将固定值除以24(一天中的小时数)而不是乘以时差来略微简化它:

CASE
  WHEN abs(RM_LIVE.TRANSACTIONLOG.TIMESTAMP
    - RM_LIVE.CRWGNDACTTIME.GNDACTSTART) <= 11/24 THEN 'LESS"
ELSE 'MORE'
END AS "mORE/LESS",