ahmedrahil786

MFT/car, MAU/car , NR/Car - car model wise - Rahil

Jun 2nd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. set @start = "2017-01-01 00:00:00";
  2. select
  3. A.month as month, A.year as year, A.active_zones, A.active_cars, A.cumulative_cars,
  4. IFNULL(D.nuse,0) as resv, IFNULL(round(D.Dur,2),0) as dur, 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,
  5. IFNULL(B.ActiveMembers,0) as WAU, IFNULL(C.totalmfts,0) as MFTs,
  6. #IFNULL(round(((D.nuse)/A.cumulative_cars),2),0) as Res_PCPD,IFNULL(round(((D.Dur)/A.cumulative_cars),2),0) as Dur_PCPD ,IFNULL(round(((D.ad_sdur)/A.cumulative_cars),2),0) as adj_dur_PCPD,
  7. ROUND(IFNULL(B.ActiveMembers/A.cumulative_cars,0),2) as Wau_PCPD, ROUND(IFNULL(C.totalmfts/B.ActiveMembers,0),2) as MFT_rate,
  8. IFNULL(round(((D.rental - D.discount)/B.ActiveMembers),2),0) as Netrev_perWAU,
  9. IFNULL(round(((D.rental - D.discount)/A.cumulative_cars),2),0) as Netrev_PCPD , ROUND(IFNULL(D.nuse/B.ActiveMembers,0),2) as resv_perWAU
  10. from
  11. #### Number of cars
  12. (select
  13. month(czl.log_date + interval 8 hour ) as month,
  14. year(czl.log_date + interval 8 hour ) as Year,
  15. count(distinct czl.zone_id) as active_zones,
  16. count(distinct czl.car_id) as active_cars,
  17. count(distinct czl.id) as cumulative_cars
  18. from car_zone_logs czl
  19. left join cars cr on cr.id = czl.car_id
  20. left join car_classes cc on cc.id = cr.car_class_id
  21. left join zones z on czl.zone_id = z.id
  22. where czl.zone_state= 'normal'
  23. and czl.car_state = 'normal'
  24. and czl.log_date + interval 8 hour >= @start
  25. and z.id not in (2, 3, 101)
  26. and cc.id in (1)
  27. group by 1,2
  28. order by 2 asc , 1 asc) A
  29. left join
  30. ############## Number of Active Members
  31. (select
  32. month(r.return_at + interval '8' hour) as month,
  33. year(r.return_at + interval '8' hour) as year,
  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. left join cars c on c.id = r.car_id
  39. left join car_classes cc on cc.id = c.car_class_id
  40. join members m
  41. on m.id = r.member_id and m.imaginary = 'normal'
  42. where r.return_at + interval '8' hour >= @start
  43. and cc.id in (1)
  44. group by 1,2
  45. order by 2 asc , 1 asc) B
  46. on A.month = B.month and A.year = B.year
  47. left join
  48. ######### Number of MFTs
  49. (select A.month as month,
  50. A.year as year,
  51. count(A.mid) as totalmfts
  52. from
  53. (select distinct c.member_id as mid, month(c.created_at + interval '8' hour) as month, year(c.created_at + interval '8' hour) as year
  54. from charges c
  55. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  56. left join
  57. (select distinct r.member_id as mid, cc.id as ccid,
  58. min(r.id) as rid
  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 c on c.id = r.car_id
  63. left join car_classes cc on cc.id = c.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 and cc.id in (1)
  65. group by r.member_id) B
  66. on A.mid = B.mid
  67. where B.ccid in (1)
  68. group by 1,2
  69. order by 2 asc , 1 asc ) C
  70. on A.month = C.month and A.year = C.year
  71. left join
  72. ####### for Number of Hours
  73. (select month(r.return_at+interval 8 hour) as month,
  74. year(r.return_at+interval 8 hour) as year,
  75. count(r.id) as nuse,
  76. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  77. 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,
  78. 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,
  79. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  80. #count(distinct m.id) as mau
  81. from reservations r, members m , zones z , cars c , car_classes cc
  82. where r.member_id = m.id
  83. and r.start_zone_id = z.id
  84. and r.car_id = c.id
  85. and c.car_class_id = cc.id
  86. and r.state = 'completed'
  87. and m.imaginary in ('normal', 'sofam')
  88. and r.return_at+interval 8 hour >= @start
  89. and r.start_zone_id not in (2, 3, 101)
  90. and cc.id in (1)
  91. group by 1,2
  92. order by 2 asc , 1 asc) D
  93. on D.month = A.month and D.year = A.year
  94. group by A.month, A.year
  95. order by 2 asc , 1 asc
Advertisement
Add Comment
Please, Sign In to add comment