ahmedrahil786

AGYL Info - Distance/Vehicle

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