Find date gaps with mysql

前端 未结 3 1994
有刺的猬
有刺的猬 2021-01-03 02:38

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:



        
3条回答
  •  余生分开走
    2021-01-03 03:02

    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
    

提交回复
热议问题