开发者

SQL Server, TimeSpans

开发者 https://www.devze.com 2023-04-09 22:04 出处:网络
I am having a trouble in querying my database.. I have a table : **Room** +----------------------------------+

I am having a trouble in querying my database.. I have a table :

**Room**
 +----------------------------------+
 |RoomNo---RoomStatusID---RoomTypeID|
 |  1   ---     1      ---    1     |
 |  2   ---     1      ---    1     |
 +----------------------------------+

**ClientRoom**
 +--------------------------------------------------------------------+
 |ClientRoomNo---RoomNo--------ArrivalDate-------------DepartureDat开发者_C百科e  |
 |  1         ---  1   ---- 2011-10-03 1:00:00 ---- 2011-10-03 4:00:00|
 |  2         ---  1   ---- 2011-10-03 5:00:00 ---- 2011-10-03 8:00:00|
 +--------------------------------------------------------------------+

If I use this query =

SELECT Room.RoomNo, RoomType.RoomType
FROM Room INNER JOIN
RoomType ON Room.RoomTypeNo = RoomType.RoomTypeNo
FULL OUTER JOIN ClientRoom ON Room.RoomNo = ClientRoom.RoomNo
WHERE
(((ClientRoom.ArrivalDate <= '10-03-2011 1:00:00' AND
ClientRoom.ArrivalDate <= '10-03-2011 5:00:00') AND
(ClientRoom.DepartureDate <= '10-03-2011 1:00:00' AND
ClientRoom.DepartureDate <= '10-03-2011 5:00:00'))
OR Room.RoomStatusId  = 1)

It would return

**ClientRoom**
     +--------------------------------------------------------------------+
     |ClientRoomNo---RoomNo--------ArrivalDate-------------DepartureDate  |
     |  2         ---  1   ---- 2011-10-03 5:00:00 ---- 2011-10-03 8:00:00|
     +--------------------------------------------------------------------+

Because it reached up to 5:00am in the morning. But when I change the Arrival and Departure to.. 5:00 - 8:00.. RoomNo 1 is still in the results. I already tried NOT BETWEEN :(


You did not explain what result you want in what circumstances. But this condition

ClientRoom.ArrivalDate <= '10-03-2011 1:00:00' AND 
ClientRoom.ArrivalDate <= '10-03-2011 5:00:00'

looks fishy, because the first part is redundant. You could simply say

    ClientRoom.ArrivalDate <= '10-03-2011 5:00:00'

Same thing for the next part:

ClientRoom.DepartureDate <= '10-03-2011 1:00:00' AND
ClientRoom.DepartureDate <= '10-03-2011 5:00:00'

This is the same as just saying:

ClientRoom.DepartureDate <= '10-03-2011 5:00:00'

Edit: From the comments I read it, that you have a given timespan (a,d) and want to check for each row, if your timespan overlaps with the timespan of the row (A,D). You want to see only non-overlapping rows.

This means your WHERE clause must be like this quasicode:

WHERE d <= A -- my departure will be before the rows arrival
   OR a >= D -- my arrival is behind the rows departure

In full terms:

WHERE (    '10-03-2011 5:00:00' <= ClientRoom.ArrivalDate 
        OR '10-03-2011 1:00:00' >=  ClientRoom.DepartureDate 
      )
    OR Room.RoomStatusId = 1

Edit 2:

What does the query with this fixes? "Show me all schedule rows, which do not conflict with my schedule." But the query should be "Show me the rooms which do not have any conflicts with my schedule." That is a completely different beast but easily translated into SQL:

SELECT 
    Room.RoomNo
FROM Room
WHERE NOT EXISTS (
    SELECT 1 
    FROM ClientRoom
    WHERE 
       Room.RoomNo = ClientRoom.RoomNo
       AND (   '10-03-2011 5:00:00' <= ClientRoom.ArrivalDate 
            OR '10-03-2011 1:00:00' >=  ClientRoom.DepartureDate 
           )
)
OR Room.RoomStatusId  = 1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号