ahmedrahil786

ZONE MFT Pricing - Work on it

Feb 14th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. set @start := '2019-07-1';
  2. select
  3. A.city, A.name as region, A.Week as month, A.year as year, A.number_of_cars, A.cumulative_cars, IFNULL(B.ActiveMembers,0) as WAU, IFNULL(C.totalmfts,0) as MFTs, IFNULL(D.nuse,0) as resv,
  4. IFNULL(round(D.Dur,2),0) as duration, IFNULL(round(D.adj_dur,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 ,
  5. ROUND(IFNULL(B.ActiveMembers/A.number_of_cars,0),2) as Wau_percar,IFNULL(round(((D.rental - D.discount)/D.adj_dur),2),0) as Netrev_P_Hour,
  6. ROUND(IFNULL(C.totalmfts/B.ActiveMembers,0),2) as MFT_perMAU, IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PC_PD , ROUND(IFNULL(D.nuse/A.number_of_cars,0),2) as resv_perCar,
  7. ROUND(IFNULL(D.nuse/B.ActiveMembers,0),2) as resv_perWAU
  8. from
  9. #### Number of cars
  10. (select
  11. month(czl.log_date + interval 8 hour ) as week,
  12. z.city as city,
  13. z.region as name,
  14. year(czl.log_date + interval 8 hour ) as Year,
  15. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  16. from car_zone_logs czl
  17. left join cars cr on cr.id = czl.car_id
  18. left join car_classes cc on cc.id = cr.car_class_id
  19. left join zones z on czl.zone_id = z.id
  20. where czl.zone_state= 'normal'
  21. and czl.car_state = 'normal'
  22. and czl.zone_state = 'normal'
  23. and czl.log_date + interval 8 hour >= @start
  24. group by 1,3
  25. order by 1,2 desc) A
  26. left join
  27. ############## Number of Active Members
  28. (select
  29. month(r.return_at + interval '8' hour) as week,
  30. z.region as name,
  31. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  32. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  33. from reservations r
  34. join zones z on z.id = r.start_zone_id
  35. join members m
  36. on m.id = r.member_id and m.email not like '%socar.my%' and m.imaginary = 'normal'
  37. where r.return_at + interval '8' hour >= @start
  38. group by 1,2
  39. order by 1 asc) B
  40. on A.Week = B.Week and A.name = B.name
  41. left join
  42. ######### Number of MFTs
  43. (select
  44. A.month as week,
  45. B.name as name,
  46. count(A.mid) as totalmfts
  47. from
  48. (select
  49. distinct c.member_id as mid, Month(c.created_at + interval '8' hour) as month, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  50. from charges c
  51. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  52. left join
  53. (select
  54. distinct r.member_id as mid, min(r.id) as rid,z.region as name,
  55. z.id as zid
  56. from reservations r
  57. join members m on r.member_id = m.id
  58. join zones z on r.start_zone_id = z.id
  59. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  60. group by r.member_id) B
  61. on A.mid = B.mid
  62. where B.zid is not null
  63. group by A.week,name ) C
  64. on A.Week = C.Week and A.name = C.name
  65. left join
  66. ####### for Number of Hours
  67. (select
  68. month(r.return_at) as week, z.region as name, count(r.id) as nuse,
  69. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  70. 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,
  71. 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,
  72. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  73. #count(distinct m.id) as mau
  74. from reservations r, members m , zones z
  75. where r.member_id=m.id
  76. and r.start_zone_id = z.id
  77. and r.way in ('round','d2d','oneway')
  78. and r.state = 'completed'
  79. and m.imaginary in ('normal', 'sofam')
  80. and r.return_at+interval 8 hour >= @start
  81. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  82. group by week, name) D
  83. on D.week = A.week and D.name = A.name
  84. where A.week > month(now()) - 3
  85. group by A.week, A.name
  86. order by 3 asc
Advertisement
Add Comment
Please, Sign In to add comment