首页 > 解决方案 > 在 sql/oracle 中比较时间

问题描述

如果 take_time 列 ie-(complete_time- Create_time) 小于或等于 240 小时,则 SLA 为“输入”,否则为“输出”

例子:

创建日期时间 完成日期时间
06-02-2021 12:01:00 06-02-2021 14:02:00
07-02-2021 1:01:00 07-02-2021 10:02:00

预期输出:

创建日期时间 完成日期时间 采取_时间 服务水平协议
06-02-2021 12:01:00 06-02-2021 14:02:00 02:01:00
01-02-2021 01:01:00 10-02-2021 01:40:00 240:39:00 出去

所以我们必须首先计算所用时间,然后在所用时间的基础上计算 SLA。create_Day_time 和 complete_date_time 按照 oracle 存储日期的格式存储在数据库中。

标签: sqloracle

解决方案


你实际上不需要计算taken_time来做到这一点。相反,您可以只使用日期算术和比较:

select t.*,
       (case when complete_date_time > Create_Date_time + interval '240' hour
             then 'Out' else 'In'
        end) as in_out
from t;

您也可以将其表示为:

select t.*,
       (case when complete_date_time - Create_Date_time > 240 / 24
             then 'Out' else 'In'
        end) as in_out
from t;

推荐阅读