Guest User

Untitled

a guest
Jan 17th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. id | timestamp | status
  2. -------------------------------------------
  3. A | 2018-01-01 03:00:00 | GOOD
  4. A | 2018-01-01 04:00:00 | BAD
  5. A | 2018-01-01 05:00:00 | GOOD
  6. A | 2018-01-01 09:00:00 | BAD
  7. A | 2018-01-01 09:15:00 | BAD
  8. A | 2018-01-01 13:00:00 | GOOD
  9. A | 2018-01-01 14:00:00 | GOOD
  10. B | 2018-02-01 09:00:00 | GOOD
  11. B | 2018-02-01 10:00:00 | BAD
  12.  
  13. id | timestamp | status | bad_status_count
  14. ----------------------------------------------------------------
  15. A | 2018-01-01 03:00:00 | GOOD | 0
  16. A | 2018-01-01 04:00:00 | BAD | 1
  17. A | 2018-01-01 05:00:00 | GOOD | 1
  18. A | 2018-01-01 09:00:00 | BAD | 1
  19. A | 2018-01-01 09:15:00 | BAD | 2
  20. A | 2018-01-01 13:00:00 | GOOD | 0
  21. A | 2018-01-01 14:00:00 | GOOD | 0
  22. B | 2018-02-01 09:00:00 | GOOD | 0
  23. B | 2018-02-01 10:00:00 | BAD | 1
  24.  
  25. SELECT
  26. id,
  27. timestamp,
  28. status
  29. count(status) over(partition by id order by timestamp range between interval '3' hour and current_row) as bad_status_count
  30. from table
  31.  
  32. select t.*,
  33. sum(case when status = 'Bad' then 1 else 0 end) over
  34. (partition by id
  35. order by hours
  36. range between -3 and current row
  37. ) as bad_status
  38. from (select t.*,
  39. date_diff(hour, '2000-01-01', timestamp) as hours
  40. from t
  41. ) t;
Add Comment
Please, Sign In to add comment