sql 时间相减 得到 毫秒/秒/分钟/小时/天等

来源:互联网 发布:淘宝货源批发网 编辑:程序博客网 时间:2024/04/28 19:54

想起几个月之前接到一个需求,要统计服务器对每个请求的大致处理时间。幸好对每个请求都记了日志到数据库。因此写了一个时间相减的sql。

select t1.id id,       t1.requrl requsetUrl,       t1.requesttime requestTime,       t1.responsetime responseTime,       extract(day from t1.intv) * 24 * 60 * 60 * 1000 +       extract(hour from t1.intv) * 60 * 60 * 1000 +       extract(minute from t1.intv) * 60 * 1000 +       extract(second from t1.intv) * 1000 operTime  from (SELECT t.resp_time - t.reqs_time AS intv,               t.id id,               t.reqs_url reqUrl,               t.reqs_time requestTime,               t.resp_time responseTime          from t_log t         where t.resp_time is not null) t1 order by operTime desc 

得到结果如图:
这里写图片描述

上图中,数据库是oralce, t_log 的两个时间字段,reqs_time(请求时间),resp_time(响应时间) 的字段类型都是 timestamp。 operTime 就是两个时间相减得到的值(毫秒),差值有点儿大(测试环境数据,请忽略,哈哈)。毫秒值都得到了,那秒、分钟、小时等肯定也能得到了。

1 0
原创粉丝点击