my table measurements(oracle sql 12)有3列:dt-measurement timestamp、measurement-value、threshold-upper limit。
有时测量值高于阈值。尝试计算测量值高于阈值时的时间段。
dt测量阈值
—————————————————————————————————————————————————
2016年8月4日01:10 60,5 70,0
2016年8月4日01:20 65,5 70,0
2016年8月4日01:30 68,1 70,0
04.08.16 01:40 70,1*70,0//时段开始
2016年8月4日01:50 70,1*70,0
04.08.16 02:00 70,75*70,0//期末
2016年8月4日02:10 53,5 70,0
2016年8月4日02:20 50,15 70,0
2016年8月4日02:30 52,15 70,0
2016年8月4日02:40 53,15 70,0
预期结果(02:00-01:40=00:20):
持续时间开始结束
——+——————————————————————————————————————————————————---
00:20 04.08.16 01:40 04.08.16 02:00

最佳答案

不需要使用两个行号,可以通过累加法直接使用:

select max(dt) - min(dt) as duration, min(dt), max(dt)
from (select *, row_number() over (order by dt) as seq,
                sum(case when measurement > threshold then 1 else 0 end) over(order by dt) as grp
      from table
     ) t
where measurement > threshold
group by (seq - grp);

关于sql - Oracle SQL:计算值超过阈值的时间段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50968096/

10-13 00:54