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

想起几个月之前接到一个需求,要统计服务器对每个请求的大致处理时间。幸好对每个请求都记了日志到数据库。因此写了一个时间相减的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 就是两个时间相减得到的值(毫秒),差值有点儿大(测试环境数据,请忽略,哈哈)。毫秒值都得到了,那秒、分钟、小时等肯定也能得到了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值