ahmedrahil786

Daily Index 2

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