ahmedrahil786

Healthy Status by Zone - Rahil

Mar 11th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. ###### With all the week
  2.  
  3. set @start := '2020-01-1';
  4. select
  5. concat(A.year,"-",A.weekofyear) as week_year,A.city,A.name as zone_name, 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.name as name,
  16. z.region as region,
  17. year(czl.log_date + interval 8 hour ) as Year,
  18. count(distinct czl.zone_id) as number_of_Zones,
  19. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  20. from car_zone_logs czl
  21. left join cars cr on cr.id = czl.car_id
  22. left join car_classes cc on cc.id = cr.car_class_id
  23. left join zones z on czl.zone_id = z.id
  24. where czl.zone_state= 'normal'
  25. and czl.car_state = 'normal'
  26. and czl.log_date + interval 8 hour >= @start
  27. and z.id not in (2, 3, 101)
  28. group by 1,3,4
  29. order by 1,2 desc) A
  30. left join
  31. ############## Number of Active Members
  32. (select
  33. weekofyear(r.return_at + interval '8' hour) as weekofyear, z.name as name,
  34. z.region as region,
  35. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.member_id end) as ActiveMembers,
  36. count(distinct case when r.state NOT IN ('canceled', 'fail') then r.id end) as resv
  37. from reservations r
  38. join zones z on z.id = r.start_zone_id
  39. join members m
  40. on m.id = r.member_id and m.imaginary = 'normal'
  41. where r.return_at + interval '8' hour >= @start
  42. group by 1,2
  43. order by 1 asc) B
  44. on A.weekofyear = B.weekofyear and A.name = B.name
  45. left join
  46. ######### Number of MFTs
  47. (select
  48. A.weekofyear as weekofyear,
  49. B.name as name,
  50. B.region as region,
  51. count(A.mid) as totalmfts
  52. from
  53. (select
  54. distinct c.member_id as mid, weekofyear(c.created_at + interval '8' hour) as weekofyear, WEEKOFYEAR(c.created_at + interval '8' hour) as Week
  55. from charges c
  56. where c.kind = 'subscriptionFee' and c.created_at + interval '8' hour >= @start) A
  57. left join
  58. (select
  59. distinct r.member_id as mid, min(r.id) as rid, z.name as name, z.region as region,
  60. z.id as zid
  61. from reservations r
  62. join members m on r.member_id = m.id
  63. join zones z on r.start_zone_id = z.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.weekofyear, name ) C
  69. on A.weekofyear = C.weekofyear and A.name = C.name
  70. left join
  71. ####### for Number of Hours
  72. (select
  73. weekofyear(r.return_at + interval 8 hour) as weekofyear, z.name as name, z.region as region, count(r.id) as nuse,
  74. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  75. 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,
  76. 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,
  77. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount
  78. #count(distinct m.id) as mau
  79. from reservations r, members m , zones z
  80. where r.member_id = m.id
  81. and r.start_zone_id = z.id
  82. and r.state = 'completed'
  83. and m.imaginary in ('normal', 'sofam')
  84. and r.return_at+interval 8 hour >= @start
  85. and r.start_zone_id not in (2, 3, 101)
  86. group by weekofyear , name) D
  87. on D.weekofyear = A.weekofyear and D.name = A.name
  88. group by week_year, A.name
  89. order by 1 asc ;
Advertisement
Add Comment
Please, Sign In to add comment