i am doing a project on hotel reservation.in that i have to show room vacancy of selected week in the page.i am taking the booked room count from hotel Booking table.below is few feilds in booking table.
my problem is i have to get sum of HB_Noofrooms between hb_chkDt and hb_chkoDt of each date in selected week.
ex. suppose if i take current date and check it,then i have to get 2 as sum.
please hel开发者_Go百科p me to solve this.
DECLARE @BeginDate date='2010-01-01'
DECLARE @EndDate date='2011-01-01'
; With Dates([Date]) as (
SELECT @BeginDate
UNION ALL
SELECT DATEADD(Day,1,Date) FROm Dates
WHERE Dates.date<@EndDate
)
SELECT Dates.Date,(SELECT COUNT(HB_No_Of_Rooms) FROM @MyTable WHERE HB_CkdDT>=Dates.date
AND HB_ChkODt<=Dates.Date
GROUP By HB_No_Of_Rooms
)
FROM Dates
OPTION(MAXRECURSION 0)
If you want to retrive the total number of rooms occupied in a week:
select sum(HB_NoOfRooms) from Booking
where (hb_chkDt >= CONVERT(DATETIME, '2011-09-25 00:00:00') and hb_chkDt < CONVERT(DATETIME, '2011-10-01 00:00:00'))
or (HB_ChkODt >= CONVERT(DATETIME, '2011-09-25 00:00:00') and HB_ChkODt < CONVERT(DATETIME, '2011-10-01 00:00:00'))
If you want to check the total number of room occupied today:
select sum(HB_NoOfRooms) FROM Booking
where HB_ChkDt <=SYSDATETIME() AND HB_ChkODt >SYSDATETIME()
If you will pass date as parameter for which you need to get vacancy then it's simple query :
SELECT SUM(HB_NoOfRooms) FROM Booking
WHERE HB_ChkDt <= @PassedDt AND HB_ChkODt >= @PassedDt
But if you pass range of dates and want to get date wise vacancy for all in between dates then need to make some complex query.
UPDATE : Code to get vacancy for multiple date ranges :
DECLARE @strSQL NVARCHAR(MAX)
DECLARE @StartDt DATETIME
DECLARE @EndDt DATETIME
CREATE TABLE #Booking (HB_Id INT, HB_ChkDt DATETIME, HB_ChkODt DATETIME, HB_NoOfRooms INT)
INSERT INTO #Booking VALUES (61, '2011-09-07 13:00:00','2011-09-08 13:00:00',1)
INSERT INTO #Booking VALUES (67, '2011-09-27 13:00:00','2011-09-28 2:00:00',1)
INSERT INTO #Booking VALUES (68, '2011-09-27 13:00:00','2011-09-28 2:00:00',1)
INSERT INTO #Booking VALUES (69, '2011-09-28 13:00:00','2011-09-29 2:00:00',1)
SET @StartDt = '2011-09-27'
SET @EndDt = '2011-09-29'
WHILE @StartDt <= @EndDt
BEGIN
IF @strSQL IS NULL OR @strSQL = ''
SET @strSQL = 'SELECT ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''' AS Dt, ISNULL(SUM(HB_NoOfRooms),0) AS Vacancy FROM #Booking WHERE CONVERT(VARCHAR(10),HB_ChkDt,102) <= ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''' AND CONVERT(VARCHAR(10),HB_ChkODt,102) >= ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''''
ELSE
SET @strSQL = @strSQL + ' UNION ALL SELECT ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''' AS Dt, ISNULL(SUM(HB_NoOfRooms),0) AS Vacancy FROM #Booking WHERE CONVERT(VARCHAR(10),HB_ChkDt,102) <= ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''' AND CONVERT(VARCHAR(10),HB_ChkODt,102) >= ''' + CAST(CONVERT(VARCHAR(10),@StartDt,102) AS VARCHAR(10)) + ''''
SET @StartDt = DATEADD(D,1,@StartDt)
END
EXEC (@strSQL)
DROP TABLE #Booking
精彩评论