首页 > 解决方案 > 使用oracle计算两个日期之间的时间差(以秒为单位)

问题描述

我正在计算 6 到 59 秒之间的总记录总和,但不起作用。

sum(
    case trunc((d.start_date-s.end_date)*24*60*60)
    when between 6 and 59 then 1
    end) as onemin
from employee; 

标签: sqloracle

解决方案


您需要在when关键字之后移动条件:

sum( 
    case when trunc((d.start_date-s.end_date)*24*60*60) between 6 and 59 then 1 end
) as onemin

您可能还想如下表达条件,避免对每条记录应用函数(这可能更有效):

sum (
    case when
        d.start_date - s.end_date >= 6 / 24 / 60 / 60
        and d.start_date - s.end_date < 60 / 24 / 60 / 60 -- or 1 / 24 / 60
    then 1 end
) onemin

推荐阅读