sql - 在 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 存储日期的格式存储在数据库中。
解决方案
你实际上不需要计算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;
推荐阅读
- python - 如何在 datetime python 中更改时间并添加循环
- mysql - 3 个连接表中的 SQL 数学函数
- kotlin - 是否可以为 Kotlin 覆盖 ENUM 的名称?
- c++17 - 大批
向量 VS2019 上的 range v3 风格 - python - 字典中的 Python 条件键/值
- c++ - 负回文数失败
- jmeter - 如何在 JMeter 上处理此问题 - System.FormatException:Base-64 字符数组或字符串的长度无效
- php - 使用基于值的 Jquery 对 DataTable 进行排序
- google-cloud-functions - Firebase 功能:部署版本或时间戳(用于日志记录)
- asp.net-mvc-4 - 如何在同一网站的 IronPython 和 asp.net mvc 之间共享 cookie?