ahmedrahil786

F2 - Z_index

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