简体   繁体   中英

How to get the minimum value for a given time-period

I have a table with equipment failure and resolved date. Until the failure is resolved, entries for each day will show as failed. Once the issue is resolved data will start from the next failure date. Below is an example

在此处输入图像描述

I want an output which will give me the first failure time for each resolved timestamp like在此处输入图像描述

I tried to do a left join between Resolved timestamp and failure dates AND take the min but that doesn't work.

Consider below approach

select type, 
  max(timestamp) resolved_timestamp,
  min(timestamp) first_failure_timestamp
from (  
  select *, countif(status='resolved') over win as grp
  from your_table
  window win as (partition by type order by timestamp rows between unbounded preceding and 1 preceding)
)
group by type, grp    

if applied to sample data in y our question - output is

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM