ahmedrahil786

Concentrated MFT - Special Pricing MFTs

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