Oracle SQL在两个时间戳之间的天数差异

时间:2017-07-25 04:34:08

标签: sql oracle

 event            start                             end
 event147         22-JUL-18 11.55.02.000000000  23-JUL-18 12.45.12.000000000 
 event148         24-JUL-18 13.12.45.000000000  25-JUL-18 18.33.05.000000000 

上面是md_events表,我需要在事件结束147和事件148开始之间得到天数(没有时间部分)。两者都是时间戳。

我试过,但是收到错误(与numtodsinterval相同)

select dsintervaltonum(me2.start- me1.end, 'day') as days
  from md_events me1, md_events me2
 where me1.event  = 'event147'
   and me2.event =  'event148'

此查询还返回时间部分

 select to_char(me2.start- me1.end, 'DDD') as days
      from md_events me1, md_events me2
     where me1.event  = 'event147'
       and me2.event =  'event148'

3 个答案:

答案 0 :(得分:1)

将两个时间戳转换为日期,然后直接区分它们:

call :LIB_TIME

echo Date is: %_DATE%
echo Time is: %_TIME_SC%
echo.
echo Date is: %_YYYY%-%_MM%-%_DD%
echo Time is: %_HR%:%_MN%:%_SC%

答案 1 :(得分:1)

你也可以使用这个:

SELECT EXTRACT(DAY FROM (me2.start- me1.end))
FROM md_events me1
    CROSS JOIN md_events me2
where
    me1.event  = 'event147' and
    me2.event =  'event148'

答案 2 :(得分:0)

我的系统中没有Oracle。所以我将提供一种方法/逻辑来使用SQLITE找到您的解决方案。使用ORACLE语法改变SQLITE语法!!

SELECT y.start,x.endd, y.start-x.endd as diff FROM (SELECT a.* FROM NAMES AS a WHERE a.event='event147') AS x, (SELECT b.* FROM NAMES AS b WHERE b.event='event148') AS y;