简体   繁体   English

如何在SQL Server中的两个表中循环获取日期范围

[英]How to loop for date range within two tables in sql server

Please, can anyone give me any idea how to get all dates individually within a date range of another table. 请任何人告诉我如何在另一个表格的日期范围内分别获取所有日期。

How to do 'not include 18 and 19' also: How do I rewrite stored procedure not to count as available middle dates(18,19) of Reservation table status 'Occupied'. 如何也“不包括18和19”:如何重写存储过程,以不算作保留表状态为“已占用”的可用中间日期(18,19)。

I have date range in Reservation table of : 我的预定表中有日期范围:

CheckInDate (07/16/2014 12:00:00 AM)
CheckOutDate (07/20/2014 13:00:00 PM)

RoomNumber     CheckInDate                CheckOutDate               Status
   204         07/16/2014 12:00:00 AM     07/20/2014 13:00:00 PM     Occupied  

But in my AvailableTest table, there is no date range. 但是在我的AvailableTest表中,没有日期范围。 All records are going individual date. 所有记录都是单独的日期。 But in Reservation table they do not have individual date. 但是在保留表中,它们没有单独的日期。 They have range and how do I match that date range and my individual date from AvailableTest table. 它们具有范围,我该如何匹配该日期范围和我来自AvailableTest表的单个日期。

RoomNumber     CheckInDate                CheckOutDate               Status
   204         07/16/2014 12:00:00 AM     07/16/2014 13:00:00 PM     Available
   204         07/17/2014 12:00:00 AM     07/17/2014 13:00:00 PM     Available
   204         07/18/2014 12:00:00 AM     07/18/2014 13:00:00 PM     Available
   204         07/19/2014 12:00:00 AM     07/19/2014 13:00:00 PM     Available
   204         07/20/2014 12:00:00 AM     07/20/2014 13:00:00 PM     Available

If someone check for available room within range of 07/18/2014 to 07/24/2014 如果有人检查2014年7月18日至2014年7月24日范围内的可用客房

   204         07/18/2014 12:00:00 AM     07/18/2014 13:00:00 PM     Available
   204         07/19/2014 12:00:00 AM     07/19/2014 13:00:00 PM     Available
   204         07/20/2014 12:00:00 AM     07/20/2014 13:00:00 PM     Available

should not available anymore but when I try to query out they only take out 应该不再可用了,但是当我尝试查询时,它们只会取出

   204         07/20/2014 12:00:00 AM     07/20/2014 13:00:00 PM     Available

and 18 and 19 is still come out as available. 而18和19仍然可用。

It looks like they only know and try to match with checkindate and checkoutdate only so they check 16 and 20 within the date range of 18 to 24(checking available). 看来他们只知道并尝试仅与checkindatecheckoutdate匹配,因此他们在18到24的日期范围内检查16和20(可用检查)。 They don't know 18 and 19 is occupied also. 他们不知道18和19也被占用。

Here is my query 这是我的查询

select distinct (at.RoomNumber), at.RoomID, '', '', '', '', '', '', at.CheckInDate, at.CheckOutDate
from AvailableTest at                       
   join Room r on r.RoomID = at.RoomID 
   join Reservations res on res.RoomID =r.RoomID where 
   ((at.CheckInDate between '07/18/2014 13:00:00 PM' and '07/24/2014 12:00:00 AM')
        or (at.CheckOutDate between '07/18/2014 13:00:00 PM' and '07/24/2014 12:00:00 AM'))
        and at.CheckOutDate not in ( SELECT CheckOutDate  
    FROM reservations rs WHERE ((CheckInDate BETWEEN '07/18/2014 13:00:00 PM' AND '07/24/2014 12:00:00 AM') OR (CheckOutDate BETWEEN '07/18/2014 13:00:00 PM' AND '07/24/2014 12:00:00 AM')) ) and at.CheckInDate  not in ( SELECT  CheckInDate FROM reservations rs WHERE ((at.CheckInDate BETWEEN '07/18/2014 13:00:00 PM' AND  '07/24/2014 12:00:00 AM') OR (at.CheckOutDate BETWEEN '07/18/2014 13:00:00 PM' AND '07/24/2014  12:00:00 AM')) )'

I think you are looking to only get rows from the AvailableTest table for dates that there are no rows in the Reservations table. 我认为您希望仅从AvailableTest表中获取行,以获取保留表中没有行的日期。

SELECT DISTINCT
    r.RoomNumber,
    at.RoomID ,
    '' ,
    '' ,
    '' ,
    '' ,
    '' ,
    '' ,
    at.CheckInDate ,
    at.CheckOutDate
FROM    AvailableTest at
    INNER JOIN Room r
        ON r.RoomID = at.RoomID
    INNER JOIN Reservations res
        ON res.RoomID = r.RoomID
WHERE at.CheckInDate NOT BETWEEN res.CheckInDate AND res.CheckOutDate

There are some design issues with your availabletest table. availabletest表存在一些设计问题。 As it seems you have a checkin/checkout columns though there are only rows for a single date. 似乎您有一个签入/签出列,尽管单个日期只有一行。 Just adding a level of complication that isn't needed. 只需添加不需要的复杂程度即可。 I know this as I worked for a Hotel Management company for 10 years. 我在一家酒店管理公司工作了10年,这一点我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM