Created
April 26, 2020 13:51
-
-
Save 3YH/301ced5cd0035e6e9eb3a3fb57aea22f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with t1 (id, stat, ts, ndx) as ( | |
select monitor_id, monitor_status, performed_at, row_number() over(order by performed_at) | |
from uptime_checks | |
), | |
t2 (id, stat, startts, endts) as ( | |
SELECT t1.id, t1.stat, t1.ts, coalesce(t2.ts, now()) | |
from t1 | |
left join t1 t2 | |
on t2.ndx = t1.ndx + 1 | |
), | |
t3 (stat, totaltime) as ( | |
select stat, sum(timestampdiff(second, startts, endts)) | |
from t2 | |
group by stat | |
) | |
select stat, totaltime | |
from t3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment