ahmedrahil786

ARPH,RESPCPD, NRPCPD by City and Carname

Mar 16th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. set @startdate1 = '2020-01-01';
  2. set @startdate2 = '2020-01-31';
  3. use socar_malaysia;
  4. Select I.month as month, I.city as city, I.car_name as car_name, Q.cumulative_cars as cars, Round(I.net_rev,2) as Net_rev, ROUND(I.Total_reservations/Q.cumulative_cars,2) as RESPCPD, ROUND(I.net_rev/Q.cumulative_cars,2) as RevPCPD,
  5. Round(I.Dur/Q.cumulative_cars,2) as DURPCPD, Round(I.net_rev/I.ad_sdur,2) as NRPH
  6. from
  7. ( select A.city as city, A.car_name as car_name, Month(A.return_date) as month, count(A.rid2) as Total_reservations, count(distinct A.mid) as Total_Unique_Members, sum(A.distance) as distance, sum(A.dur) as dur, sum(A.ad_sdur) as ad_sdur,
  8. IFNULL(Sum(E.charges),0) as gross_rev,
  9. IFNULL(sum(F.coupon_Spent),0) as Coupon_Spent,
  10. round((IFNULL(sum(E.charges),0) - IFNULL(sum(F.coupon_Spent),0)),2) as net_rev from
  11. (select
  12. distinct r.id as rid2, r.member_id as mid, r.way as way, cc.car_name as car_name, z.city as city,
  13. IFNULL(sum(ra.mileage),0) as distance,
  14. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  15. 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,
  16. Date(r.return_at + interval '8' hour) as return_Date,
  17. month(r.return_at + interval '8' hour) as month
  18. from reservations r left join members m on r.member_id = m.id
  19. left join reservation_appendixes ra on ra.reservation_id = r.id
  20. left join cars cr on cr.id = r.car_id
  21. left join car_classes cc on cc.id = cr.car_class_id
  22. left join zones z on r.start_zone_id = z.id
  23. where r.state in ('completed')
  24. and r.return_at + interval 8 hour >= @startdate1
  25. and r.return_at + interval 8 hour <= @startdate2
  26. and m.imaginary in ('sofam', 'normal')
  27. and r.member_id not in ('125', '127')
  28. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  29. and z.city in ('KL','SL','PG','JB')
  30. group by rid2) A
  31. left join
  32. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  33. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  34. group by rid) E on A.rid2 = E.rid
  35. left join
  36. (select p.reservation_id as rid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon_Spent from payments p
  37. where p.state = 'normal' and p.paid_type = 'coupon'
  38. group by p.reservation_id) F on A.rid2 = F.rid
  39. group by 1,2,3 ) I
  40. left join
  41. (select
  42. month(czl.log_date + interval 8 hour ) as month,
  43. z.city as city,
  44. cc.car_name as car_name,
  45. year(czl.log_date + interval 8 hour ) as Year,
  46. count(distinct czl.car_id) as number_of_cars , count(distinct czl.id) as cumulative_cars
  47. from car_zone_logs czl
  48. left join cars cr on cr.id = czl.car_id
  49. left join car_classes cc on cc.id = cr.car_class_id
  50. left join zones z on czl.zone_id = z.id
  51. where czl.zone_state= 'normal'
  52. and czl.car_state = 'normal'
  53. and czl.log_date + interval 8 hour >= @startdate1
  54. and czl.log_date + interval 8 hour <= @startdate2
  55. and z.id not in (2, 3, 101)
  56. group by 1,2,3,4
  57. order by 1,2 desc) Q
  58. on Q.month = I.month and Q.car_name = I.car_name and Q.city = I.city
  59. group by 1,2,3
Advertisement
Add Comment
Please, Sign In to add comment