ahmedrahil786

Loui Supply Dash - 2 Queries to Optimize Ads on MFT regions

Mar 4th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. ###### With all the week
  2.  
  3. set @start := '2020-01-1';
  4. select
  5. concat(A.weekofyear,"-",A.year) as week_year,A.city, A.region as region,A.number_of_Zones as number_of_Zones,A.number_of_cars as ending_cars, A.cumulative_cars,IFNULL(D.nuse,0) as resv,IFNULL(round(D.Dur,2),0) as duration,
  6. IFNULL(round(D.ad_sdur,2),0) as adj_dur,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 ,
  7. IFNULL(B.ActiveMembers,0) as WAU, IFNULL(C.totalmfts,0) as MFTs,ROUND(IFNULL(B.ActiveMembers/A.number_of_cars,0),2) as Wau_percar,ROUND(IFNULL(C.totalmfts/B.ActiveMembers,0),2) as MFT_perMAU,
  8. ROUND(IFNULL((D.rental - D.discount)/B.ActiveMembers,0),2) as NR_perMAU,IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PC_PD ,
  9. ROUND(IFNULL(D.nuse/B.ActiveMembers,0),2) as resv_perWAU
  10. from
  11. #### Number of cars
  12. (select
  13. weekofyear(czl.log_date + interval 8 hour ) as weekofyear,
  14. z.city as city,
  15. z.region as region,
  16. year(czl.log_date + interval 8 hour ) as Year,
  17. count(distinct czl.zone_id) as number_of_Zones,
  18. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  19. from car_zone_logs czl
  20. left join cars cr on cr.id = czl.car_id
  21. left join car_classes cc on cc.id = cr.car_class_id
  22. left join zones z on czl.zone_id = z.id
  23. where czl.zone_state= 'normal'
  24. and czl.car_state = 'normal'
  25. and czl.log_date + interval 8 hour >= @start
  26. and z.id not in (2, 3, 101)
  27. group by 1,3,4
  28. order by 1,2 desc) A
  29. left join
  30. ############## Number of Active Members
  31. (select
  32. weekofyear(r.return_at + interval '8' hour) as weekofyear,
  33. z.region as region,
  34. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  35. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  36. from reservations r
  37. join zones z on z.id = r.start_zone_id
  38. join members m
  39. on m.id = r.member_id and m.imaginary = 'normal'
  40. where r.return_at + interval '8' hour >= @start
  41. group by 1,2
  42. order by 1 asc) B
  43. on A.weekofyear = B.weekofyear and A.region = B.region
  44. left join
  45. ######### Number of MFTs
  46. (select
  47. A.weekofyear as weekofyear,
  48. B.region as region,
  49. count(A.mid) as totalmfts
  50. from
  51. (select
  52. distinct c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as weekofyear, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  53. from charges c
  54. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  55. left join
  56. (select
  57. distinct r.member_id as mid, min(r.id) as rid,z.region as region,
  58. z.id as zid
  59. from reservations r
  60. join members m on r.member_id = m.id
  61. join zones z on r.start_zone_id = z.id
  62. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  63. group by r.member_id) B
  64. on A.mid = B.mid
  65. where B.zid is not null
  66. group by A.weekofyear,region ) C
  67. on A.weekofyear = C.weekofyear and A.region = C.region
  68. left join
  69. ####### for Number of Hours
  70. (select
  71. weekofyear(r.return_at + interval 8 hour) as weekofyear, z.region as region, count(r.id) as nuse,
  72. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  73. round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
  74. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)) as rental,
  75. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  76. #count(distinct m.id) as mau
  77. from reservations r, members m , zones z
  78. where r.member_id = m.id
  79. and r.start_zone_id = z.id
  80. and r.state = 'completed'
  81. and m.imaginary in ('normal', 'sofam')
  82. and r.return_at+interval 8 hour >= @start
  83. and r.start_zone_id not in (2, 3, 101)
  84. group by weekofyear , region) D
  85. on D.weekofyear = A.weekofyear and D.region = A.region
  86. group by week_year, A.region
  87. order by 1 asc ;
  88.  
  89.  
  90. ##########################################################################################################################
  91.  
  92. Query with only last week data
  93.  
  94. set @start := '2020-01-1';
  95. Select I.* from
  96. (select
  97. concat(A.weekofyear,"-",A.year) as week_year,A.city, A.region as region,A.number_of_Zones as number_of_Zones,A.number_of_cars as ending_cars, A.cumulative_cars,IFNULL(D.nuse,0) as resv,IFNULL(round(D.Dur,2),0) as duration,
  98. IFNULL(round(D.ad_sdur,2),0) as adj_dur,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 ,
  99. IFNULL(B.ActiveMembers,0) as WAU, IFNULL(C.totalmfts,0) as MFTs,ROUND(IFNULL(B.ActiveMembers/A.number_of_cars,0),2) as Wau_percar,ROUND(IFNULL(C.totalmfts/B.ActiveMembers,0),2) as MFT_perMAU,
  100. ROUND(IFNULL((D.rental - D.discount)/B.ActiveMembers,0),2) as Net_Revenue_perMAU,IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PC_PD ,
  101. ROUND(IFNULL(D.nuse/B.ActiveMembers,0),2) as resv_perWAU
  102. from
  103. #### Number of cars
  104. (select
  105. weekofyear(czl.log_date + interval 8 hour ) as weekofyear,
  106. z.city as city,
  107. z.region as region,
  108. year(czl.log_date + interval 8 hour ) as Year,
  109. count(distinct czl.zone_id) as number_of_Zones,
  110. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  111. from car_zone_logs czl
  112. left join cars cr on cr.id = czl.car_id
  113. left join car_classes cc on cc.id = cr.car_class_id
  114. left join zones z on czl.zone_id = z.id
  115. where czl.zone_state= 'normal'
  116. and czl.car_state = 'normal'
  117. and czl.log_date + interval 8 hour >= @start
  118. and z.id not in (2, 3, 101)
  119. group by 1,3,4
  120. order by 1,2 desc) A
  121. left join
  122. ############## Number of Active Members
  123. (select
  124. weekofyear(r.return_at + interval '8' hour) as weekofyear,
  125. z.region as region,
  126. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  127. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  128. from reservations r
  129. join zones z on z.id = r.start_zone_id
  130. join members m
  131. on m.id = r.member_id and m.imaginary = 'normal'
  132. where r.return_at + interval '8' hour >= @start
  133. group by 1,2
  134. order by 1 asc) B
  135. on A.weekofyear = B.weekofyear and A.region = B.region
  136. left join
  137. ######### Number of MFTs
  138. (select
  139. A.weekofyear as weekofyear,
  140. B.region as region,
  141. count(A.mid) as totalmfts
  142. from
  143. (select
  144. distinct c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as weekofyear, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  145. from charges c
  146. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  147. left join
  148. (select
  149. distinct r.member_id as mid, min(r.id) as rid,z.region as region,
  150. z.id as zid
  151. from reservations r
  152. join members m on r.member_id = m.id
  153. join zones z on r.start_zone_id = z.id
  154. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  155. group by r.member_id) B
  156. on A.mid = B.mid
  157. where B.zid is not null
  158. group by A.weekofyear,region ) C
  159. on A.weekofyear = C.weekofyear and A.region = C.region
  160. left join
  161. ####### for Number of Hours
  162. (select
  163. weekofyear(r.return_at + interval 8 hour) as weekofyear, z.region as region, count(r.id) as nuse,
  164. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  165. round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
  166. sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage','insurance')),0)) as rental,
  167. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  168. #count(distinct m.id) as mau
  169. from reservations r, members m , zones z
  170. where r.member_id = m.id
  171. and r.start_zone_id = z.id
  172. and r.state = 'completed'
  173. and m.imaginary in ('normal', 'sofam')
  174. and r.return_at+interval 8 hour >= @start
  175. and r.start_zone_id not in (2, 3, 101)
  176. group by weekofyear , region) D
  177. on D.weekofyear = A.weekofyear and D.region = A.region
  178. group by week_year, A.region
  179. order by 1 asc) I
  180. where I.week_year = concat(Weekofyear(now())-1,"-",year(now()))
Advertisement
Add Comment
Please, Sign In to add comment