ahmedrahil786

Zone MFT MAU Pricing - To test MFT Prices

May 13th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. set @startdate = '2019-04-01 00:00:00';
  2. select
  3. date_format(@startdate, '%Y-%m') as rmonth, cz.team as team, cz.region as region, cz.zid, cz.zname,
  4. count(distinct cz.zid) as ending_zone, count(distinct cz.cid) as ending_car,
  5. count(distinct cz.ncar) as cumulative_car,
  6. ifnull(sum(rm.nuse),0) as rsv, ifnull(sum(dur),0) as dur, ifnull(sum(adj_dur),0) as adj_dur,
  7. sum(rm.rental) as rental, sum(rm.discount) as discount,
  8. sum(rm.rental) - sum(rm.discount) as nr,
  9. ifnull(sum(rm.mau),0) as mau,
  10. A.mft
  11. from
  12. (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, c.id as ncar
  13. from car_zone_logs c, zones z
  14. where c.zone_id=z.id
  15. and date_sub(c.log_date, interval 1 day) >= @startdate
  16. and date_sub(c.log_date, interval 1 day) < date_add(@startdate, interval 1 month)
  17. and z.id not in (2,3,101,383,378,697,712,818)
  18. group by date1, region, zid, cid)cz
  19. left join
  20. (select
  21. 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,
  22. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  23. 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,
  24. 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,
  25. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount,
  26. count(distinct m.id) as mau
  27. from reservations r, members m
  28. where r.member_id=m.id
  29. and r.way in ('round','d2d','oneway')
  30. and r.state = 'completed'
  31. and m.imaginary in ('normal', 'sofam')
  32. and r.return_at+interval 8 hour >= @startdate
  33. and r.return_at+interval 8 hour < date_add(@startdate, interval 1 month)
  34. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  35. group by date2, zid2, cid2)rm
  36.  
  37. on (date_sub(cz.date1,interval 1 day)=rm.date2 and cz.cid=rm.cid2)
  38.  
  39. left join
  40.  
  41. (select uu.*, ifnull(cc.mft,0) as mft
  42.  
  43. from(
  44. select z.zone_id as zid
  45.  
  46. from car_zone_logs z
  47.  
  48. where
  49. date_sub(z.log_date, interval 1 day) >= @startdate
  50. and date_sub(z.log_date, interval 1 day) < @startdate+interval 1 month
  51. and z.zone_id not in (2,3,101,383,378,697,712,818,786)
  52.  
  53. group by zid
  54. order by zid desc
  55.  
  56. ) uu
  57. left join
  58. (select zid2, count(distinct if(before_use=0, mid, null)) as mft
  59. from
  60. (select
  61. z.id as zid2, r.member_id as mid,
  62. (select count(id)
  63. from reservations
  64. where member_id=r.member_id
  65. and state='completed'
  66. and return_at+interval 8 hour < @startdate
  67. and start_zone_id not in (2,3,101,383,378,697,712,818,786)) as before_use
  68. from reservations r, members m, zones z
  69. where
  70. r.state='completed'
  71. and r.member_id=m.id
  72. and r.start_zone_id = z.id
  73. and m.imaginary in ('normal', 'sofam')
  74. and r.return_at+interval 8 hour >= @startdate
  75. and r.return_at+interval 8 hour < @startdate+interval 1 month
  76. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  77.  
  78. group by r.member_id
  79. ) temp
  80.  
  81. where
  82. before_use=0
  83.  
  84. group by zid2) cc
  85. on (uu.zid = cc.zid2)
  86.  
  87. group by zid
  88. order by zid) A
  89.  
  90. on A.zid = cz.zid
  91. group by cz.zid;
Advertisement
Add Comment
Please, Sign In to add comment