ahmedrahil786

Cumulative Car - Loui

May 10th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 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. from
  10. (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
  11. from car_zone_logs c, zones z
  12. where c.zone_id=z.id
  13. and date_sub(c.log_date, interval 1 day) >= @startdate
  14. and date_sub(c.log_date, interval 1 day) < date_add(@startdate, interval 1 month)
  15. and z.id not in (2,3,101,383,378,697,712,818)
  16. group by date1, region, zid, cid)cz
  17. left join
  18. (select
  19. 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,
  20. sum(timestampdiff(minute, r.start_at, r.return_at)/60) as dur,
  21. 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,
  22. 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,
  23. sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)) as discount,
  24. count(distinct m.id) as mau
  25. from reservations r, members m
  26. where r.member_id=m.id
  27. and r.way in ('round','d2d','oneway')
  28. and r.state = 'completed'
  29. and m.imaginary in ('normal', 'sofam')
  30. and r.return_at+interval 8 hour >= @startdate
  31. and r.return_at+interval 8 hour < date_add(@startdate, interval 1 month)
  32. and r.start_zone_id not in (2,3,101,383,378,697,712,818)
  33. group by date2, zid2, cid2)rm
  34.  
  35. on (date_sub(cz.date1,interval 1 day)=rm.date2 and cz.cid=rm.cid2)
  36. group by cz.zid;
Advertisement
Add Comment
Please, Sign In to add comment