I have a set of period something like
PERIODID PERIODSTART PERIODEND PRICE STARTDOW
1 2012-12-01 2012-12-10 10 6
2 2012-12-11 2012-12-20 20 -1
3 2012-12-21 2012-12-30 30 -1
Meaning that reservation in period 1 must start on saturday, but not for periods 2 and 3.
If I have a reservation from 2012-12-10 - 2012-12-15 I want to> - filter periods for days (not a problem) - check if reservation STARTS on saturday. Filter should be only for top (or first row) and I am not sure how to do that. If reservation does not start on saturday none of the rows should be returned.
I tried
select * from periods p
where
((@ReservationStart between p.periodstart and p.periodend)
or
(@ReservationEnd between p.periodstart and p.periodend))
and ((select top 1 datepart(weekday, startdow) from periods where p.hotelID = period.hotelID order by period.periodstart) in (@datepart(weekday, @ReservationStart), -1))
Is there a way to do it better, or to optimize code better for large amount of data?