简体   繁体   English

如果存在多个日期间隔,如何找到最适合哪个日期间隔的给定日期

[英]How to find given date best fit between which date interval if there are more than one date interval is there

i am having two table one is having check in checkout dates and other information about client and other table is having rates room type hotel id and dates from when to when rates are valid. 我有两个表,一个正在检查结帐日期,其他有关客户的信息,另一个表正在对房间类型的酒店ID以及房价从何时到何时有效的房价进行收费。 now the problem is this if rate table has three start and end date and check in date lies in three of those date ranges it shows the three records while the requirement is it should choose nearest start date For example is 现在的问题是,如果费率表具有三个开始日期和结束日期,并且签入日期位于这些日期范围中的三个日期范围内,则它显示三个记录,而要求是它应选择最近的开始日期

start date  end date   room rate  
2013-10-01  2014-03-31 2000
2013-11-01  2013-12-20 2500
2013-12-21  2013-12-31 3000

and check in date is 25-12-2013 it should show record with only date range 3 which best fits to it my sql query is below ` 并签入日期为2013年12月25日,它应仅显示日期范围为3的记录,这最适合它,而我的sql查询位于“

 select b.*, r.* from(select a.Enquiry_Id,a.Ckeck_In,a.check_Out,a.Place,a.Hotel_Name,a.Meal_Plan,a.Room_Type,a.Occupancy_Type,a.Extra_Bed,a.Room_QT,h.grade,
 h.addres, h.Hotel_ID, a.ChildWithBed,a.childAgeWithoutBed,
 a.Inclusion, a.No_Night from Accomodation a inner join hotel h on
 a.Hotel_Name = h.Hotel_Name where a.Enquiry_Id = '0128201408') b inner
 join  RoomType r on b.Hotel_ID = r.Hotel_ID where r.Name = b.Room_Type
 and b.Ckeck_In between r.StartSeason and r.EndSeason order by Ckeck_`In

Try smth like this: 像这样尝试smth:

with (
    select 
        a.Enquiry_Id,a.Ckeck_In,a.check_Out,a.Place,
        a.Hotel_Name,a.Meal_Plan,a.Room_Type,
        a.Occupancy_Type,a.Extra_Bed,a.Room_QT,h.grade,
        h.addres, h.Hotel_ID, a.ChildWithBed,a.childAgeWithoutBed,
        a.Inclusion, a.No_Night 
    from Accomodation a 
    join hotel h on
        a.Hotel_Name = h.Hotel_Name 
    where 
        a.Enquiry_Id = '0128201408'
) temp
select
    temp.*, r.*
from temp
join (
    select 
        r.Hotel_ID, temp.Enquiry_Id, min(datediff(day, r.StartSeason, temp.Check_In)) MinDateDiff
    from temp
    join RoomType r on 
        temp.Hotel_ID = r.Hotel_ID 
    where 
        r.Name = temp.Room_Type
        and temp.Ckeck_In between r.StartSeason and r.EndSeason
    group by
        r.Hotel_ID, temp.Enquiry_Id
) x on
    temp.Enquiry_Id = x.Enquiry_Id
    and temp.Hotel_ID = x.Hotel_ID
join RoomType r on 
    x.Hotel_ID = r.Hotel_ID 
where 
    r.Name = temp.Room_Type
    and temp.Ckeck_In between r.StartSeason and r.EndSeason
    and datediff(day, r.StartSeason, temp.Check_In) = x.MinDateDiff
order by
    temp.Check_In

I'm calculating min datediff between StartSeason and Check_In, put it in a subquery and then rely on this datediff to get only the nearest records. 我正在计算StartSeason和Check_In之间的最小datediff,将其放在子查询中,然后依靠此datediff仅获取最近的记录。 I could make mistakes in the subquery grouping and in the outmost join. 我可能在子查询分组和最外面的联接中犯错误。 Just check it and correct if needed. 只需检查一下,并在需要时进行更正。

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

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