ahmedrahil786

Region , Zone - Wise Reservation Dump

Sep 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. ############################### Region wise Reservation Dump
  2.  
  3.  
  4. set @start := '2019-1-1';
  5. select
  6. A.city,A.region, A.month, A.number_of_cars, A.cumulative_cars, IFNULL(B.ActiveMembers,0) as MAU, IFNULL(C.totalmfts,0) as MFTs, IFNULL(D.nuse,0) as resv,
  7. IFNULL(round(D.Dur,2),0) as duration, IFNULL(round(D.rental,2),0) as gross_rev, IFNULL(round(D.discount,2),0) as coupon , IFNULL(round((D.rental - D.discount),2),0) as Net_Rev , IFNULL(B.ActiveMembers/A.number_of_cars,0) as Mau_percar,
  8. IFNULL(C.totalmfts/B.ActiveMembers,0) as MFT_perMAU, IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PC_PD , IFNULL(D.nuse/A.number_of_cars,0) as resv_perCar,
  9. IFNULL(D.nuse/B.ActiveMembers,0) as rentals_perMAU
  10. from
  11. #### Number of cars
  12. (select
  13. month(czl.log_date + interval '8' hour) as month, count(distinct czl.id) as cumulative_cars,count(distinct czl.car_id) as number_of_cars,
  14. z.id as zid, z.name as name, z.city as city, z.region as region
  15. from car_zone_logs czl
  16. join cars ca on ca.id = czl.car_id
  17. join car_classes cc on cc.id = ca.car_class_id
  18. join zones z on z.id = czl.zone_id
  19. Where czl.log_date >= @start
  20. and czl.zone_id not in (2,3,101,383,378,712,818,786)
  21. group by month,region) A
  22. left join
  23. ############## Number of Active Members
  24. (select
  25. month(r.return_at + interval '8' hour) as month,
  26. z.region as region,
  27. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  28. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  29. from reservations r
  30. join members m
  31. on m.id = r.member_id
  32. left join zones z on z.id = r.start_zone_id
  33. where r.return_at + interval '8' hour >= @start
  34. and m.email not like '%socar.my%' and m.imaginary = 'normal'
  35. group by 1,2
  36. order by 1 asc) B
  37. on A.month = B.month and A.region = B.region
  38. left join
  39. ######### Number of MFTs
  40. (select
  41. A.month as month,
  42. IFNULL(B.zid,"") as zid,
  43. IFNULL(B.region,"") as region,
  44. count(A.mid) as totalmfts
  45. from
  46. (select
  47. distinct c.member_id as mid, Month(c.created_at + interval '8' hour) as month, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  48. from charges c
  49. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  50. left join
  51. (select
  52. distinct r.member_id as mid, min(r.id) as rid,
  53. z.id as zid,z.region as region
  54. from reservations r
  55. join members m on r.member_id = m.id
  56. join zones z on r.start_zone_id = z.id
  57. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  58. group by r.member_id) B
  59. on A.mid = B.mid
  60. where B.zid is not null
  61. group by A.month,region ) C
  62. on A.month = C.month and A.region = C.region
  63. left join
  64. ####### for Number of Hours
  65. (select
  66. month(r.return_at) as month, z.region as region, count(r.id) as nuse,
  67. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  68. sum((timestampdiff(minute, r.start_at, r.end_at)/60)/24)*12 + if(mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)>=12,12,mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)) as adj_dur,
  69. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)) as rental,
  70. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  71. #count(distinct m.id) as mau
  72. from reservations r, members m , zones z
  73. where r.member_id=m.id
  74. and r.start_zone_id = z.id
  75. and r.way in ('round','d2d','oneway')
  76. and r.state = 'completed'
  77. and m.imaginary in ('normal', 'sofam')
  78. and r.return_at+interval 8 hour >= @start
  79. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  80. group by month, region) D
  81. on D.month = A.month and D.region = A.region
  82. where A.month > month(now()) - 9
  83. group by A.month, A.region
  84. order by 3 desc;
  85.  
  86. ############################### Zone wise Reservation Dump
  87.  
  88. set @start := '2019-1-1';
  89. select
  90. A.city,A.region, A.zid, A.name as zonename, A.month, A.number_of_cars, A.cumulative_cars, IFNULL(B.ActiveMembers,0) as MAU, IFNULL(C.totalmfts,0) as MFTs, IFNULL(D.nuse,0) as resv,
  91. IFNULL(round(D.Dur,2),0) as duration, IFNULL(round(D.rental,2),0) as gross_rev, IFNULL(round(D.discount,2),0) as coupon , IFNULL(round((D.rental - D.discount),2),0) as Net_Rev , IFNULL(B.ActiveMembers/A.number_of_cars,0) as Mau_percar,
  92. IFNULL(C.totalmfts/B.ActiveMembers,0) as MFT_perMAU, IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PC_PD , IFNULL(D.nuse/A.number_of_cars,0) as resv_perCar,
  93. IFNULL(D.nuse/B.ActiveMembers,0) as rentals_perMAU
  94. from
  95. #### Number of cars
  96. (select
  97. month(czl.log_date + interval '8' hour) as month, count(distinct czl.id) as cumulative_cars,count(distinct czl.car_id) as number_of_cars,
  98. z.id as zid, z.name as name, z.city as city, z.region as region
  99. from car_zone_logs czl
  100. join cars ca on ca.id = czl.car_id
  101. join car_classes cc on cc.id = ca.car_class_id
  102. join zones z on z.id = czl.zone_id
  103. Where czl.log_date >= @start
  104. and czl.zone_id not in (2,3,101,383,378,712,818,786)
  105. group by month,zid) A
  106. left join
  107. ############## Number of Active Members
  108. (select
  109. month(r.return_at + interval '8' hour) as month,
  110. r.start_zone_id as zid,
  111. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  112. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  113. from reservations r
  114. join members m
  115. on m.id = r.member_id
  116. where r.return_at + interval '8' hour >= @start
  117. and m.email not like '%socar.my%' and m.imaginary = 'normal'
  118. group by 1,2
  119. order by 1 asc) B
  120. on A.month = B.month and A.zid = B.zid
  121. left join
  122. ######### Number of MFTs
  123. (select
  124. A.month as month,
  125. IFNULL(B.zid,"") as zid,
  126. count(A.mid) as totalmfts
  127. from
  128. (select
  129. distinct c.member_id as mid, Month(c.created_at + interval '8' hour) as month, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  130. from charges c
  131. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  132. left join
  133. (select
  134. distinct r.member_id as mid, min(r.id) as rid,
  135. z.id as zid
  136. from reservations r
  137. join members m on r.member_id = m.id
  138. join zones z on r.start_zone_id = z.id
  139. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  140. group by r.member_id) B
  141. on A.mid = B.mid
  142. where B.zid is not null
  143. group by A.month,zid ) C
  144. on A.month = C.month and A.zid = C.zid
  145. left join
  146. ####### for Number of Hours
  147. (select
  148. month(r.return_at) as month, r.start_zone_id as zid, count(r.id) as nuse,
  149. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  150. sum((timestampdiff(minute, r.start_at, r.end_at)/60)/24)*12 + if(mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)>=12,12,mod((timestampdiff(minute, r.start_at, r.end_at)/60), 24)) as adj_dur,
  151. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)) as rental,
  152. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  153. #count(distinct m.id) as mau
  154. from reservations r, members m , zones z
  155. where r.member_id=m.id
  156. and r.start_zone_id = z.id
  157. and r.way in ('round','d2d','oneway')
  158. and r.state = 'completed'
  159. and m.imaginary in ('normal', 'sofam')
  160. and r.return_at+interval 8 hour >= @start
  161. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  162. group by month, zid) D
  163. on D.month = A.month and D.zid = A.zid
  164. where A.month > month(now()) - 9
  165. group by A.month, A.zid
  166. order by 5 desc
Add Comment
Please, Sign In to add comment