ahmedrahil786

Rahil - Mineng Query 1

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