ahmedrahil786

MFT Potential by car model - Rahil for Prashant

Apr 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. set @start := '2017-07-1';
  2. select
  3. A.car_name as car_name, A.month 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,
  4. IFNULL(round(((C.totalmfts)/A.cumulative_cars),2),0) as MFT_per_Car ,
  5. ROUND(IFNULL(B.ActiveMembers/A.number_of_cars,0),2) as Wau_percar,
  6. ROUND(IFNULL(C.totalmfts/B.ActiveMembers,0),2) as MFT_perMAU
  7. from
  8. #### Number of cars
  9. (select
  10. month(czl.log_date + interval 8 hour ) as month,
  11. year(czl.log_date + interval 8 hour ) as year,
  12. cc.car_name as car_name,
  13. z.region as region,
  14. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  15. from car_zone_logs czl
  16. left join cars cr on cr.id = czl.car_id
  17. left join car_classes cc on cc.id = cr.car_class_id
  18. left join zones z on czl.zone_id = z.id
  19. where czl.zone_state= 'normal'
  20. and czl.car_state = 'normal'
  21. and czl.log_date + interval 8 hour >= @start
  22. and z.id not in (2, 3, 101)
  23. group by 1,2,3
  24. order by 1,2 desc) A
  25. left join
  26. ############## Number of Active Members
  27. (select
  28. month(r.return_at + interval '8' hour) as month,
  29. Year(r.return_at + interval '8' hour) as year,
  30. cc.car_name as car_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. left join cars cr on cr.id = r.car_id
  36. left join car_classes cc on cc.id = cr.car_class_id
  37. join members m
  38. on m.id = r.member_id and m.imaginary = 'normal'
  39. where r.return_at + interval '8' hour >= @start
  40. group by 1,2,3
  41. order by 1 asc) B
  42. on A.month = B.month and A.car_name = B.car_name and B.year = A.year
  43. left join
  44. ######### Number of MFTs
  45. (select
  46. A.month as month,
  47. A.year as year,
  48. B.car_name as car_name,
  49. count(A.mid) as totalmfts
  50. from
  51. (select
  52. distinct c.member_id as mid, Month(c.created_at + interval '8' hour) as month, Year(c.created_at + interval '8' hour) as year
  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,cc.car_name as car_name,
  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. left join cars cr on cr.id = r.car_id
  63. left join car_classes cc on cc.id = cr.car_class_id
  64. where m.imaginary in ('sofam', 'normal') and r.member_id not in ('125', '127') and r.start_at + interval '8' hour >= @start
  65. group by r.member_id) B
  66. on A.mid = B.mid
  67. where B.zid is not null
  68. group by A.month, A.year , car_name ) C
  69. on A.month = C.month and A.car_name = C.car_name and A.year = C.year
  70. group by A.month, A.year , A.car_name
  71. order by 2 asc , 3 asc
Advertisement
Add Comment
Please, Sign In to add comment