ahmedrahil786

Supply vs Demand Query - BY region , car and Hour

Mar 13th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. select
  2. r.id as rid,
  3. r.state as status,
  4. #c.id as carid,
  5. #cc.id as carclassid,
  6. z.region as regionname,
  7. date(r.occupy_start_at + interval '8' hour) as day,
  8. TIMESTAMPDIFF(hour,CONVERT_TZ(r.start_at, '+00:00', '+8:00'),CONVERT_TZ(r.end_at, '+00:00', '+8:00')) as ridelength,
  9. hour(r.occupy_start_at + interval '8' hour) as startingtime,
  10. #TIMESTAMPDIFF(minute,r.start_at + interval '8' hour,r.end_at + interval '8' hour) as ridelengthinminutes,
  11. cc.car_name as carname,
  12. count(distinct case when r.state = 'completed' then r.id end) as Bookings,
  13.  
  14. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 00:00'
  15. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 00:05' then r.id end) as 00am,
  16.  
  17. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 01:00'
  18. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 01:05' then r.id end) as 01am,
  19.  
  20. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 02:00'
  21. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 02:05' then r.id end) as 02am,
  22.  
  23. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 03:00'
  24. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 03:05' then r.id end) as 03am,
  25.  
  26. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 04:00'
  27. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 04:05' then r.id end) as 04am,
  28.  
  29. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 05:00'
  30. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 05:05' then r.id end) as 05am,
  31.  
  32. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 06:00'
  33. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 06:05' then r.id end) as 06am,
  34.  
  35. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 07:00'
  36. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 07:05' then r.id end) as 07am,
  37.  
  38. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 08:00'
  39. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 08:05' then r.id end) as 08am,
  40.  
  41. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 09:00'
  42. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 09:05' then r.id end) as 09am,
  43.  
  44. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 10:00'
  45. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 10:05' then r.id end) as 10am,
  46.  
  47. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 11:00'
  48. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 11:05' then r.id end) as 11am,
  49.  
  50. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 12:00'
  51. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 12:05' then r.id end) as 12am,
  52.  
  53. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 13:00'
  54. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 13:05' then r.id end) as 13am,
  55.  
  56. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 14:00'
  57. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 14:05' then r.id end) as 14am,
  58.  
  59. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 15:00'
  60. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 15:05' then r.id end) as 15am,
  61.  
  62. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 16:00'
  63. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 16:05' then r.id end) as 16am,
  64.  
  65. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 17:00'
  66. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 17:05' then r.id end) as 17am,
  67.  
  68. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 18:00'
  69. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 18:05' then r.id end) as 18am,
  70.  
  71. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 19:00'
  72. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 19:05' then r.id end) as 19am,
  73.  
  74. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 20:00'
  75. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 20:05' then r.id end) as 20am,
  76.  
  77. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 21:00'
  78. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 21:05' then r.id end) as 21am,
  79.  
  80. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 22:00'
  81. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 22:05' then r.id end) as 22am,
  82.  
  83. count(distinct case when CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-05 23:00'
  84. and CONVERT_TZ(r.end_at, '+00:00', '+8:00') >= '2019-1-05 23:05' then r.id end) as 23am
  85.  
  86. from cars as c
  87.  
  88. join car_classes as cc
  89. on c.car_class_id = cc.id
  90.  
  91. join zones as z
  92. on z.id = c.zone_id
  93.  
  94. join reservations as r
  95. on r.car_id = c.id
  96.  
  97. join payments as p
  98. on p.reservation_id = r.id
  99.  
  100. where CONVERT_TZ(r.start_at, '+00:00', '+8:00') >= '2019-1-1 00:00'
  101. and CONVERT_TZ(r.start_at, '+00:00', '+8:00') <= '2019-1-31 00:00'
  102.  
  103. group by car_id, r.occupy_start_at + interval '8' hour,weekday(r.occupy_start_at + interval '8' hour),r.id,z.name,cc.car_name
Advertisement
Add Comment
Please, Sign In to add comment