ahmedrahil786

Cars added in a month / week - Rahil

May 27th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. set @start := '2018-12-31 00:00';
  2.  
  3. Select
  4. t1.date,
  5. t1.ncars,
  6. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff
  7. from
  8.  
  9. (select
  10. Date(date_sub(czl.log_date , interval 1 day)) as date,
  11. weekday(date_sub(czl.log_date , interval 1 day)) as weekday,
  12. count(distinct czl.car_id ) as ncars
  13. from car_zone_logs czl
  14. where czl.log_date + interval '8' hour >= @start
  15. and czl.car_state = 'normal'
  16. and czl.zone_state = 'normal'
  17. and czl.csa_state = 'normal'
  18. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  19. group by 1) t1
  20.  
  21. left join
  22.  
  23. (select
  24. Date(date_add(czl.log_date , interval 7 day)) as date,
  25. weekday(date_add(czl.log_date , interval 7 day)) as weekday,
  26. count(distinct czl.car_id ) as ncars
  27. from car_zone_logs czl
  28. where czl.log_date + interval '8' hour >= @start
  29. and czl.car_state = 'normal'
  30. and czl.zone_state = 'normal'
  31. and czl.csa_state = 'normal'
  32. and weekday(date_sub(czl.log_date , interval 1 day)) = 6
  33. group by 1) t2
  34.  
  35. on t1.date = t2.date - 1
  36.  
  37. order by t1.date ;
  38.  
  39.  
  40. set @start := '2018-12-31 00:00';
  41.  
  42. Select
  43. t1.date,
  44. t1.ncars,
  45. t1.ncars - COALESCE(t2.ncars, t1.ncars) AS diff
  46. from
  47.  
  48. (select
  49. Month(date_sub(czl.log_date , interval 1 day)) as date,
  50. LAST_DAY(date_sub(czl.log_date , interval 1 day)) as weekday,
  51. count(distinct czl.car_id ) as ncars
  52. from car_zone_logs czl
  53. where czl.log_date + interval '8' hour >= @start
  54. and czl.car_state = 'normal'
  55. and czl.zone_state = 'normal'
  56. and czl.csa_state = 'normal'
  57. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  58. group by 1) t1
  59.  
  60. left join
  61.  
  62. (select
  63. Month(date_add(czl.log_date , interval 1 month)) as date,
  64. LAST_DAY(date_add(czl.log_date , interval 1 month)) as weekday,
  65. count(distinct czl.car_id ) as ncars
  66. from car_zone_logs czl
  67. where czl.log_date + interval '8' hour >= @start
  68. and czl.car_state = 'normal'
  69. and czl.zone_state = 'normal'
  70. and czl.csa_state = 'normal'
  71. and czl.log_date = LAST_DAY(date_sub(czl.log_date , interval 1 day))
  72. group by 1) t2
  73.  
  74. on t1.date = t2.date
  75.  
  76. order by t1.date
Advertisement
Add Comment
Please, Sign In to add comment