ahmedrahil786

First Booking, Second booking by car type - Rahil

Mar 16th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2. Select L.city as city, L.car_name, Round(avg(first_booking),0) as firstbooking, Round(avg(subsequent_booking),0) as subsequent_booking from
  3. ##########################carids first booking and second booking date and time gaps in between
  4. (select A.carid as carid, A.city as city, A.car_name as car_name , A.created_date as car_created_date, B.firstbooking as first_booking_date, B.secondbooking as second_booking_date,
  5. datediff(B.firstbooking,A.created_date) first_booking,
  6. datediff(B.secondbooking,B.firstbooking) subsequent_booking
  7. from
  8. (select distinct c.id as carid, cc.car_name, z.city as city,
  9. min(date(c.created_at + interval 8 hour )) as created_date
  10. from cars c
  11. left join car_classes cc on cc.id = c.car_class_id
  12. left join zones z on c.zone_id = z.id
  13. where c.state = 'normal'
  14. group by 1) A
  15. left join
  16. (select carid as carid,
  17. Date(temp.firstdate) as firstbooking,
  18. (select min(date(r.return_at + interval 8 hour)) from reservations r where temp.carid = r.car_id and date(r.return_at + interval 8 hour) > temp.firstdate) as secondbooking
  19. from
  20. (select distinct r.car_id as carid,
  21. min(date(r.return_at + interval 8 hour)) as firstdate,
  22. r.id as rid
  23. from reservations r
  24. left join members m on m.id = r.member_id
  25. where r.state = 'completed'
  26. and m.imaginary in ('sofam', 'normal')
  27. group by 1) temp) B
  28. on A.carid = B.carid
  29. where B.firstbooking is not null) L
  30. where L.city in ('KL','SL','PG','JB')
  31. group by 1,2
Advertisement
Add Comment
Please, Sign In to add comment