February 18, 2012 at 4:56 am
Hi
Given the following table :
create table Booking
(BookingId int, RoomId int, RoomBookingStatusId byte, BookingDate datetime)
I am looking for a way to group the rows by RoomId , BookingStatusID for each continuous date range.
The following query gives me the FromDate and ToDate for each room + bookingstatus , without splitting by discontinuous date ranges.
(select RoomId,Count(RoomId) as [Days], Min([BookingDate ]) as FromDate,Max([BookingDate ]) as ToDate, RoomBookingStatusId,
from Room_Booking
group by RoomId,RoomBookingStatusId)
Any help appreciated..
February 18, 2012 at 11:06 am
This might give you an idea of how to do what you want to do.
http://www.sqlservercentral.com/articles/T-SQL/71550/
It is an excellent discussion by Jeff Moden with sample code to do what I think you want to do.
Edited spelling
February 18, 2012 at 10:58 pm
Thanks..
http://www.sqlservercentral.com/articles/T-SQL/71550 had exactly what I needed. I hadn't quite understood the "multiple objects" when I saw the post earlier..
Regards
February 19, 2012 at 5:27 am
vj6396 (2/18/2012)
Thanks..http://www.sqlservercentral.com/articles/T-SQL/71550 had exactly what I needed. I hadn't quite understood the "multiple objects" when I saw the post earlier..
Regards
There may be others with the same thought. Now if it would not be revealing of confidential data, may I ask you to help others by posting the code you write in answer to your problem, so that others may learn.
March 2, 2012 at 10:38 pm
Here's what I used
;with
groupeddates as
(
select uniquedate = [Date],
DateGroup = DATEADD(dd, - ROW_NUMBER() OVER (Partition by RoomId,RoomReservationStatusId ORDER BY RoomId, RoomReservationStatusId, ExtraBedCount, [Date]), [Date]),
RoomReservationStatusId, RoomId, ExtraBedCount
from Room_Booking
where ReservationId = @reservationId
group by RoomId, RoomReservationStatusId, ExtraBedCount, [Date]
)
June 21, 2012 at 7:36 am
@vj6396, thanks for sharing the code you used.
@celko, there's no need to be rude and dismissive about someone else's design. Some of your comments are universally valid, others are entirely personal preference. You cannot second-guess the circumstances that lead to a particular design: in my case and possibly in that of the OP, I have inherited someone else's DB design and must work within those constraints and therefore need to produce date ranges from individual date rows.
Other than the purpose of helping the OP, the next most important point of this thread is to give the rest of us a starting point for building similar solutions - particularly as this is high on the Google listings and is cross-linked elsewhere. To that effect, @vj6396's reply post adds a lot more value than yours.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply