ahmedrahil786

Fauzaan Big query - Engine off and on times

Feb 11th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. SELECT hour, round(lat, 4) as lat, round(lng, 4) as lng, count (*) as total from (
  2. SELECT *,
  3. LAG(engine,1) OVER
  4. (PARTITION BY carId ORDER BY createdAt) AS last_engine,
  5. LAG (engine, 2) OVER
  6. (PARTITION BY carId ORDER BY createdAt) as next_engine,
  7. EXTRACT(HOUR FROM createdAt) as hour
  8. FROM `my_socar_csa.csa_event`
  9. )
  10. WHERE engine = "ON"
  11. AND next_engine = "OFF"
  12. AND createdAt > '2020-1-1'
  13.  
  14. AND lat BETWEEN 2.9036133 AND 2.9398435
  15. AND lng BETWEEN 101.6292075 AND 101.6619699
  16. GROUP BY lat, lng, hour
  17. having total < 60 and total > 3
  18. order by total desc;
  19.  
  20.  
  21.  
  22. ##############################################################
  23.  
  24. Removing all filters
  25.  
  26. SELECT hour, round(lat, 4) as lat, round(lng, 4) as lng, count (*) as total from (
  27. SELECT *,
  28. LAG(engine,1) OVER
  29. (PARTITION BY carId ORDER BY createdAt) AS last_engine,
  30. LAG (engine, 2) OVER
  31. (PARTITION BY carId ORDER BY createdAt) as next_engine,
  32. EXTRACT(HOUR FROM createdAt) as hour
  33. FROM `my_socar_csa.csa_event`
  34. )
  35. WHERE engine = "ON"
  36. AND next_engine = "OFF"
  37. AND createdAt > '2020-3-17'
  38.  
  39.  
  40. GROUP BY lat, lng, hour
  41. order by total desc;
Advertisement
Add Comment
Please, Sign In to add comment