ahmedrahil786

Zone NRPCPD - Rahil

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