ahmedrahil786

Loui Query for MFTs

May 10th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. set @startDate = "2019-04-01 00:00:00";
  2. select uu.*, ifnull(cc.mft,0)
  3.  
  4. from(
  5. select z.zone_id as zid
  6.  
  7. from car_zone_logs z
  8.  
  9. where
  10. date_sub(z.log_date, interval 1 day) >= @startdate
  11. and date_sub(z.log_date, interval 1 day) < @startdate+interval 1 month
  12. and z.zone_id not in (2,3,101,383,378,697,712,818,786)
  13.  
  14. group by zid
  15. order by zid desc
  16.  
  17. ) uu
  18. left join
  19. (select zid2, count(distinct if(before_use=0, mid, null)) as mft
  20. from
  21. (select
  22. z.id as zid2, r.member_id as mid,
  23. (select count(id)
  24. from reservations
  25. where member_id=r.member_id
  26. and state='completed'
  27. and return_at+interval 8 hour < @startdate
  28. and start_zone_id not in (2,3,101,383,378,697,712,818,786)) as before_use
  29. from reservations r, members m, zones z
  30. where
  31. r.state='completed'
  32. and r.member_id=m.id
  33. and r.start_zone_id = z.id
  34. and m.imaginary in ('normal', 'sofam')
  35. and r.return_at+interval 8 hour >= @startdate
  36. and r.return_at+interval 8 hour < @startdate+interval 1 month
  37. and r.start_zone_id not in (2,3,101,383,378,697,712,818,786)
  38.  
  39. group by r.member_id
  40. ) temp
  41.  
  42. where
  43. before_use=0
  44.  
  45. group by zid2) cc
  46. on (uu.zid = cc.zid2)
  47.  
  48. group by zid
  49. order by zid
  50. ;
Advertisement
Add Comment
Please, Sign In to add comment