ahmedrahil786

F7 Monthly - F1 - 2018 - Query

Jun 11th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #reservation return at###############################################################################################################################
  2.  
  3. set @startdate1 = '2018-01-01';
  4. set @startdate2 = '2018-12-31';
  5. use socar_malaysia;
  6.  
  7. select concat(month(convert_tz(r.return_at, '+00:00', '+8:00')),"-","2019") as month,
  8. (select count(distinct czl.car_id) from car_zone_logs czl where month=month(czl.log_date+interval 8 hour) and czl.zone_state='normal' and czl.log_date + interval 8 hour >= @startdate1 and czl.log_date + interval 8 hour <= @startdate2 ) as ncars,
  9. count(r.id) as nuse,
  10. count(if(r.way='round', r.id, null)) as nuse_rnd,
  11. count(if(r.way='d2d', r.id, null)) as res_d2d,
  12. count(distinct r.car_id) as ncar_used,
  13. count(distinct z.id) as nzone,
  14. count(distinct m.id) as nmem,
  15. #count(if(r.way='oneway', r.id, null)) as res_ow,
  16. sum(ra.mileage) as rmil,
  17. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  18. round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
  19. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)),2) as revenues,
  20. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons,
  21. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)
  22. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as nr
  23.  
  24.  
  25. from reservations r, zones z, members m, reservation_appendixes ra
  26.  
  27. Where
  28. r.start_zone_id = z.id
  29. and r.member_id = m.id
  30. and r.id = ra.reservation_id
  31. and m.imaginary in ('sofam', 'normal')
  32. and r.member_id not in ('125', '127')
  33. and z.id not in (2, 3, 101)
  34. and r.state in ('completed')
  35. and r.return_at + interval 8 hour >= @startdate1
  36. and r.return_at + interval 8 hour <= @startdate2
  37.  
  38. group by month
  39. order by month desc
  40. ;
Advertisement
Add Comment
Please, Sign In to add comment