Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # VIEW: vswitcheventsdist - PASS 1 - retrieves only distinct on>off & off>on events
- SELECT a.Id,
- a.FeedId,
- a.DateTime,
- a.Value
- FROM
- (SELECT * ,
- LAG(Id) OVER (
- ORDER BY datetime) AS prev
- FROM feedsdata
- WHERE feedid = 27
- AND (Value = 'on'
- OR Value = 'off')
- ORDER BY datetime) a
- INNER JOIN feedsdata fd ON a.prev = fd.Id
- WHERE a.Value <> fd.Value
- ####################################################3
- # PASS 2 - calculate duration
- SELECT dtStart,
- dtEnd,
- duration
- FROM
- (SELECT a.datetime AS dtEnd,
- b.datetime AS dtStart,
- TIMESTAMPDIFF(MINUTE, b.datetime, a.datetime) AS duration
- FROM
- (SELECT * ,
- LAG(Id) OVER (
- ORDER BY datetime) AS prev
- FROM vswitcheventsdist) a
- INNER JOIN vswitcheventsdist b ON a.prev = b.Id
- WHERE a.Value = 'off'
- AND b.Value = 'on' ) c
- #######################
- # Result
- dtStart | dtEnd | Duration
- "2018-07-10 04:30:16" "2018-07-10 04:54:12" 23
- "2018-07-10 13:47:27" "2018-07-10 14:34:48" 47
- "2018-07-10 15:48:52" "2018-07-10 15:49:08" 0
- "2018-07-10 17:00:14" "2018-07-10 17:14:15" 14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement