ahmedrahil786

first and subsequent booking - TTT

Jun 17th, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. select lid as lid,car_brand as car_brand, car_model as car_model,
  2. Date(temp.firstdate) as firstbooking,
  3. (select min(Date(request_start_at)) from bookings b where temp.lid = b.listing_id and (b.request_start_at) > temp.firstdate) as secondbooking
  4. from
  5. (select distinct l.id as lid,
  6. br.name as car_brand, m.name as car_model,
  7. min(b.request_start_at) as firstdate
  8. from bookings b
  9. left join listings l on b.listing_id = l.id
  10. left join models m on l.model_id = m.id
  11. left join brands br on m.brand_id = br.id
  12. where b.status = 'completed'
  13. group by 1,2,3) temp
  14. order by 1 asc
Advertisement
Add Comment
Please, Sign In to add comment