ahmedrahil786

Mineng Query for Used cars on that day

Jun 13th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. ######################################################################################
  2. # # of car used for each day
  3. select temp.rday,
  4. (select count(czl.id) from car_zone_logs czl where rday=date_format(czl.log_date, "%Y-%m-%d") and czl.zone_state='normal') as ncars,
  5. temp.ncars_used,
  6. (select count(distinct car_id) from reservations r, members m
  7. where r.state = 'completed'
  8. and date_format(r.return_at+interval 8 hour, "%Y-%m-%d") = temp.rday
  9. and r.member_id=m.id
  10. and m.imaginary in ('sofam', 'normal')
  11. and r.start_zone_id not in (2,3,101)
  12. ) ncar_returned
  13. from (
  14. select rday,count(distinct cid) as ncars_used
  15. from (select date_format(dt.rday, "%Y-%m-%d") as rday, resv.rid as rid, resv.cid as cid, resv.rsh as rsh, resv.reh as reh
  16. from (select concat(date_format(r.return_at+interval 8 hour, "%Y-%m-%d"), " 00:00:00") as rday from reservations r
  17. where r.return_at is not null and r.return_at+interval 8 hour > "2019-05-01 00:00:00"
  18. group by rday
  19. ) dt,
  20. (select r.id as rid, r.car_id as cid, r.start_at+interval 8 hour as rsh, r.end_at+interval 8 hour as reh
  21. from reservations r, members m
  22. where r.member_id=m.id
  23. and m.imaginary in ('sofam', 'normal')
  24. and r.state in ('completed', 'inuse')
  25. and r.start_zone_id not in (2,3,101)
  26. and r.end_at+interval 8 hour >= "2019-05-01"
  27. ) resv
  28. where resv.rsh < (dt.rday+interval 1 day) and resv.reh > dt.rday) temp
  29. group by rday
  30. order by rday desc) temp;
Advertisement
Add Comment
Please, Sign In to add comment