2010-03-03 59 views
1

的兩列我有一個查詢的日期時間2005年比較, 我有一個表命名tblBooking 沒有在表中存在3列SQL Server的同桌

Column Names BookingID StartTime EndTime 
Datatype   int  DateTime DateTime 

的樣本數據:

BookingID StartTime EndTime 
    1   10:00  11:00 
    2   11:00  12:00 
    3   01:30  03:30 

我需要比較兩列StartTimeEndTime

查詢的價值觀:我不得不alloacte資源如。會議室這可以每天完成

between 10:00 am to 7:00 pm. 

所以我需要檢查時間的基礎上會議室的可用性。考慮所有情況。 這是要求我在接受採訪時的一個問題,我不能回答,所以我需要找到辦法如何做到這一點

+2

請發佈您迄今爲止編寫的代碼。人們通常不喜歡只爲你寫代碼。 – 2010-03-03 07:12:45

回答

0

你可以有多個條件在where子句中......
之間能比較多個日期和時間
例如:

where requestedStartTime not between startTime and endTime 
and requestedEndTime not between startTime and endTime 
1

看一看這個例子。你新來檢查重疊的時期,所以選擇與日期時間沒有結果將是正確的。

 |---|   PERIOD 
-------------------------------------- 
|---|     FINE 
    |---|    FINE 
    |------|   NOT FINE 
     |---|   NOT FINE 
     |-|   NOT FINE 
      |------|  NOT FINE 
      |---|  FINE 
       |---| FINE 
    |-------------|  NOT FINE 




DECLARE @tblBookings TABLE(
     BookingID INT, 
     StartTime DATETIME, 
     EndTime DATETIME 
) 

INSERT INTO @tblBookings SELECT 1, '01 Jan 2010 10:00:00', '01 Jan 2010 11:00:00' 
INSERT INTO @tblBookings SELECT 2, '01 Jan 2010 11:00:00', '01 Jan 2010 12:00:00' 

DECLARE @StartDate DATETIME, 
     @EndDate DATETIME 

--Inside   
SELECT @StartDate = '01 Jan 2010 10:30:00', 
     @EndDate = '01 Jan 2010 11:30:00' 

SELECT * 
FROM @tblBookings 
WHERE NOT(@EndDate <= StartTime OR @StartDate >= EndTime) 

--Overlapping left  
SELECT @StartDate = '01 Jan 2010 09:30:00', 
     @EndDate = '01 Jan 2010 10:30:00' 

SELECT * 
FROM @tblBookings 
WHERE NOT(@EndDate <= StartTime OR @StartDate >= EndTime) 

--Overlapping right  
SELECT @StartDate = '01 Jan 2010 11:30:00', 
     @EndDate = '01 Jan 2010 12:30:00' 

SELECT * 
FROM @tblBookings 
WHERE NOT(@EndDate <= StartTime OR @StartDate >= EndTime) 

--Overlapping left and right 
SELECT @StartDate = '01 Jan 2010 09:30:00', 
     @EndDate = '01 Jan 2010 12:30:00' 

SELECT * 
FROM @tblBookings 
WHERE NOT(@EndDate <= StartTime OR @StartDate >= EndTime) 

--THIS ONE IS FINE 
SELECT @StartDate = '01 Jan 2010 09:00:00', 
     @EndDate = '01 Jan 2010 10:00:00' 

SELECT * 
FROM @tblBookings 
WHERE NOT(@EndDate <= StartTime OR @StartDate >= EndTime) 
0

你可以找到指定的日期/時間範圍和現有的所有日期間所有的「衝突」 /時間範圍在您的表像這樣的查詢:

DECLARE @YourTable table (BookingID int,StartTime datetime,EndTime datetime) 

INSERT INTO @YourTable VALUES (1, '2010-03-03 10:00am', '2010-03-03 10:30am') 
INSERT INTO @YourTable VALUES (2, '2010-03-03 11:30am', '2010-03-03 01:30pm') 

DECLARE @StartDate datetime 
     ,@EndDate datetime 

SELECT @StartDate='2010-03-03 9:00am' 
     ,@EndDate='2010-03-03 10:15am' 
;WITH AllDates AS 
(
    SELECT @StartDate AS DateOf 
    UNION ALL 
    SELECT DATEADD(mi,30,DateOf) --smallest increment to consider is 30 minutes 
     FROM AllDates 
    WHERE DateOf<@EndDate 
) 
,Collisions AS 
(SELECT 
    * 
    FROM @YourTable   y 
     INNER JOIN AllDates a ON a.DateOf>=y.StartTime AND a.DateOf<=y.EndTime 
) 
SELECT * FROM Collisions 

OUTPUT:

BookingID StartTime    EndTime     DateOf 
--------- ----------------------- ----------------------- ----------------------- 
1   2010-03-03 10:00:00.000 2010-03-03 10:30:00.000 2010-03-03 10:00:00.000 
1   2010-03-03 10:00:00.000 2010-03-03 10:30:00.000 2010-03-03 10:30:00.000 

(2 row(s) affected)