ahmedrahil786

NRPCPD by Carid

Oct 24th, 2019 (edited)
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. set @startdate1 = '2020-08-01';
  2. set @startdate2 = '2020-08-31';
  3. use socar_malaysia;
  4. select Q.carid as carid, Q.zone_name as zone_name, Q.city as city, Q.car_name as car_name, Q.region as region, Q.ncars as ncars,
  5. IFNULL(P.nmem,0) as members, IFNULL(P.resv,0) as resv, IFNULL(P.dur,0) as dur, IFNULL(P.Ad_sdur,0) as Ad_sdur, IFNULL(P.distance,0) as distance,
  6. IFNULL(P.gross_rev,0) as gross_rev, IFNULL(P.coupon_spent,0) as coupon_spent, IFNULL(P.Net_rev,0) as Net_rev,
  7. ifnull(ROUND(P.resv/Q.ncars,2),0) as RESPCPD,ifnull(ROUND(P.dur/Q.ncars,2),0) as DURPCPD,ifnull(ROUND(P.net_rev/Q.ncars,2),0) as REVPCPD
  8. from
  9. (select czl.car_id as carid,z.name as zone_name,cc.car_name as car_name, z.region as region,z.city as city,
  10. count(czl.id) as ncars from car_zone_logs czl
  11. left join cars cr on cr.id = czl.car_id
  12. left join car_classes cc on cc.id = cr.car_class_id
  13. left join zones z on z.id = czl.zone_id
  14. left join cars c on c.id = czl.car_id
  15. where czl.zone_state= 'normal'
  16. and czl.log_date + interval 8 hour >= @startdate1
  17. and czl.log_date + interval 8 hour <= @startdate2
  18. and c.state = 'normal'
  19. group by 1
  20. order by 1 desc) Q
  21. left join
  22. (select I.carid as carid, Count(I.mid) as nmem,Count(I.rid) as resv, Sum(I.dur) as dur, Sum(I.ad_sdur) as Ad_sdur,Sum(I.distance) as distance, count(if(I.coupon ='none', null, I.coupon)) as coupon, Round(Sum(I.gross_rev),2) as gross_rev,
  23. ROUND(Sum(I.coupon_spent),2) as coupon_spent , ROUND(Sum(I.net_rev),2) as Net_rev from
  24. (select A.return_date as date, A.car_id as carid, A.rid as rid, A.mid as mid,A.zid as zid, A.region as region, A.city as city, A.car_name as car_name, (A.coupon) as coupon, A.dur as dur, A.ad_sdur as ad_sdur, A.distance as distance,
  25. A.nuse_rnd as nuse_rnd,A.res_d2d as res_d2d,A.res_oneway as res_oneway , A.resv_hour as res_hour, A.occupy_start_hour as start_hour, A.age as age, IFNULL(E.charges,0) as gross_rev,
  26. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  27. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  28. (select
  29. distinct r.id as rid, r.member_id as mid, z.id as zid, z.region, z.city, cc.car_name,IFNULL(co.comment,"none") as coupon,r.car_id as car_id,
  30. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  31. 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,
  32. IFNULL(sum(ra.mileage),0) as distance,
  33. count(if(r.way='round', r.id, null)) as nuse_rnd,
  34. count(if(r.way='d2d', r.id, null)) as res_d2d,
  35. count(if(r.way='oneway', r.id, null)) as res_oneway,
  36. Date(r.return_at + interval '8' hour) as return_Date,
  37. hour(r.start_at + interval '8' hour) as resv_hour,
  38. hour(r.occupy_start_at + interval '8' hour) as occupy_start_hour,
  39. (YEAR(r.start_at) - YEAR(m.birthday)) as age
  40. from reservations r left join members m on r.member_id = m.id
  41. left join zones z on z.id = r.start_zone_id
  42. left join cars cr on cr.id = r.car_id
  43. left join car_classes cc on cc.id = cr.car_class_id
  44. left join coupons co on co.reservation_id = r.id
  45. left join reservation_appendixes ra on ra.reservation_id = r.id
  46. where r.state in ('completed')
  47. and r.return_at + interval 8 hour >= @startdate1
  48. and r.return_at + interval 8 hour <= @startdate2
  49. and m.imaginary in ('sofam', 'normal')
  50. and r.member_id not in ('125', '127')
  51. and (YEAR(r.start_at) - YEAR(m.birthday)) < 100
  52. group by rid) A
  53. left join
  54. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  55. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  56. group by rid) E on A.rid = E.rid
  57. left join
  58. (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
  59. where p.state = 'normal' and p.paid_type = 'coupon'
  60. group by p.reservation_id) F on A.rid = F.rid) I
  61. where I.city not like ("-")
  62. group by I.carid
  63. order by I.carid desc) P
  64. on P.carid = Q.carid
  65. order by 1 asc
Advertisement
Add Comment
Please, Sign In to add comment