ahmedrahil786

F6 - Last 4 weeks

May 18th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. set @startdate = date_sub(CURDATE(), interval 1 day);
  2. select
  3. cz.operating,
  4. cz.zid,
  5. cz.zname,
  6. cz.carclass,
  7. cz.carname,
  8. B.daysdeployed,
  9. count(distinct cz.ncar) as ncar,
  10. round(count(distinct cz.ncar)/B.daysdeployed,2) as davg,
  11. IFNULL(sum(rm.nuse),0) as nuse,
  12. IFNULL(sum(rm.nuse_rnd),0) as nuse_rnd,
  13. IFNULL(sum(rm.res_d2d),0) as res_d2d,
  14. IFNULL(sum(rm.ncar_used),0) as ncar_used,
  15. count(distinct cz.zid) as nzone,
  16. IFNULL(sum(rm.nmem),0) as nmem,
  17. IFNULL(sum(rm.rmil),0) as rmil,
  18. round(ifnull(sum(dur),0),2) as dur,
  19. round(IFNULL(sum(rm.rental),0),2) as rental, round(IFNULL(sum(rm.discount),0),2) as discount,
  20. round(IFNULL(sum(rm.rental) - sum(rm.discount),0),2) as nr,
  21. round(IFNULL(sum(rm.nuse_rnd)/count(distinct cz.ncar),0),2) as nuse_pct
  22.  
  23.  
  24. from
  25. (select c.log_date as date1, z.city as team, z.region, z.id as zid, z.name as zname, c.car_id as cid, cc.id as carclass,
  26. cc.car_name as carname,c.id as ncar,ca.id as carsid, count(if(z.state='Normal', z.id, null)) as operating
  27. from car_zone_logs c, zones z , car_classes cc , cars ca
  28. where c.zone_id=z.id
  29. and c.car_id = ca.id
  30. and ca.car_class_id = cc.id
  31. and date_sub(c.log_date, interval 1 day) >= date_sub(@startdate, interval 4 week)
  32. and date_sub(c.log_date, interval 1 day) < @startdate
  33. and z.id not in (2,3,101,383,378,697,712,818)
  34. group by date1, region, zid, cid,carname)cz
  35.  
  36. left join
  37. (select count(distinct c.log_date) as daysdeployed, c.car_id as carid,c.zone_id as zid from car_zone_logs c
  38. where date_sub(c.log_date, interval 1 day) >= date_sub(@startdate, interval 4 week)
  39. and date_sub(c.log_date, interval 1 day) < @startdate
  40. group by c.zone_id) B
  41. on cz.zid = B.zid
  42.  
  43. left join
  44. (select
  45. date_format(r.return_at, '%Y-%m-%d') as date2, r.start_zone_id as zid2, r.car_id as cid2, count(r.id) as nuse,
  46. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  47. count(if(r.way='round', r.id, null)) as nuse_rnd,
  48. count(if(r.way='d2d', r.id, null)) as res_d2d,
  49. count(distinct r.car_id) as ncar_used,
  50. count(distinct z.id) as nzone,
  51. count(distinct m.id) as nmem,
  52. #count(if(r.way='oneway', r.id, null)) as res_ow,
  53. sum(ra.mileage) as rmil,cc.id as carclass,
  54. cc.car_name as carname,ca.id as carsid,
  55. 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,
  56. 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,
  57. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount,
  58. count(distinct m.id) as mau
  59. from reservations r, members m , reservation_appendixes ra , zones z , car_classes cc , cars ca
  60. where r.member_id=m.id
  61. and r.start_zone_id = z.id
  62. and r.id = ra.reservation_id
  63. and r.car_id = ca.id
  64. and ca.car_class_id = cc.id
  65. and r.way in ('round','d2d','oneway')
  66. and r.state = 'completed'
  67. and m.imaginary in ('normal', 'sofam')
  68. and r.return_at+interval 8 hour >= date_sub(@startdate, interval 4 week)
  69. and r.return_at+interval 8 hour < @startdate
  70. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  71. group by date2, zid2, cid2, carname)rm
  72.  
  73. on (date_sub(cz.date1,interval 1 day)=rm.date2 and cz.cid=rm.cid2 and cz.zid = rm.zid2 and cz.carsid = rm.carsid)
  74.  
  75. left join
  76.  
  77. (select uu.*, ifnull(cc.mft,0) as mft
  78.  
  79. from(
  80. select z.zone_id as zid
  81.  
  82. from car_zone_logs z
  83.  
  84. where
  85. date_sub(z.log_date, interval 1 day) >= @startdate-interval 4 week
  86. and date_sub(z.log_date, interval 1 day) < @startdate
  87. and z.zone_id not in (2,3,101,383,378,697,712,818,786)
  88.  
  89. group by zid
  90. order by zid desc
  91.  
  92. ) uu
  93. left join
  94. (select zid2, count(distinct if(before_use=0, mid, null)) as mft
  95. from
  96. (select
  97. z.id as zid2, r.member_id as mid,
  98. (select count(id)
  99. from reservations
  100. where member_id=r.member_id
  101. and state='completed'
  102. and return_at+interval 8 hour < @startdate
  103. and start_zone_id not in (2,3,101,383,378,697,712,818,786)) as before_use
  104. from reservations r, members m, zones z
  105. where
  106. r.state='completed'
  107. and r.member_id=m.id
  108. and r.start_zone_id = z.id
  109. and m.imaginary in ('normal', 'sofam')
  110. and r.return_at+interval 8 hour >= @startdate-interval 4 week
  111. and r.return_at+interval 8 hour < @startdate
  112. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  113.  
  114. group by r.member_id
  115. ) temp
  116.  
  117. where
  118. before_use=0
  119.  
  120. group by zid2) cc
  121. on (uu.zid = cc.zid2)
  122.  
  123. group by zid
  124. order by zid) A
  125.  
  126. on A.zid = cz.zid
  127. group by cz.zid,cz.carname,cz.carsid;
Advertisement
Add Comment
Please, Sign In to add comment