I would like to extract records with having an empty bookingId and get the maximum unbooked days back (from the first free day). The expected result should be:
Pls try this...
select
concat_ws(',',(concat("ID=",id)),
min(startDate),
(concat((count(*) - (endDate is null))," Days Free"))) as result
from (
select
pb1.id,
pb1.bookingDate startDate,
min(pb2.bookingDate) endDate
from
pricesBookings pb1 left join pricesBookings pb2
on pb1.id=pb2.id
and pb2.price>0
and pb2.bookingDate>pb1.bookingDate
where
pb1.price=0
group by
pb1.id,
pb1.bookingDate
) s
group by id, endDate
order by id, startDateselect
concat_ws(',',(concat("ID=",id)),
min(startDate),
(concat((count(*) - (endDate is null))," Days Free"))) as result
from (
select
pb1.id,
pb1.bookingDate startDate,
min(pb2.bookingDate) endDate
from
pricesBookings pb1 left join pricesBookings pb2
on pb1.id=pb2.id
and pb2.price>0
and pb2.bookingDate>pb1.bookingDate
where
pb1.price=0
group by
pb1.id,
pb1.bookingDate
) s
group by id, endDate
order by id, startDate