0

我有三个表 OccupancyType、RoomType 和 Hotel_MealPlan。数据是:-

占用类型数据:-

Hotel_ID    Name
       1    Double
       1    single
     680    Double
     680    single

酒店_膳食计划

Hotel_ID    Meal_Plan   rate
       1    CP          0
       1    MAP         500
     680    CP          400
     680    EP          400         
       1    EP          200

房型

Hotel_ID    Name    Rate    Season    StartSeason   EndSeason
 680    Deluxe/Ac   2300    Diwali   2013-11-01 2013-11-15
 680    Deluxe/Ac   1000    Normal    NULL          NULL
   1    Deluxe/Ac   2700    Diwali    2013-11-01    2013-11-15
   1    Deluxe/Ac   1200    Normal    NULL          NULL
   1    Deluxe/Ac   2500    New Year  2013-12-20    2014-01-10
   1    Deluxe/Ac   3800    31 Dec    2013-12-31    2013-12-31

我想要当我选择hotel_Id '1'、occupancyType Name='Double'、Meal_Plan ='MAP' 和我的房间类型名称='Deluxe/Ac' 以及 startSeason 和 EndSeason 之间的日期,或者如果开始季节为 Null 则选择 null 率。我想要这样的结果: - 下面

Name    Name    Rate    startseason endseason   Meal_Plan   rate
Double  Deluxe/Ac   1200    NULL    NULL        MAP         500

我的查询是

select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,m.rate 
from OccupancyType o 
inner join RoomType r on o.Hotel_ID = r.Hotel_ID 
inner join Hotel_MealPlan m on m.Hotel_ID = o.Hotel_ID 
where r.Hotel_ID = '1' 
and r.Name = 'Deluxe/Ac' 
and o.Name = 'Double' 
and m.Meal_Plan = 'MAP' 
and '2013-09-09' between r.startseason and r.endseason 
or r.startseason is null 
and r.endseason is null 

我的结果是:-

Name    Name    Rate    startseason endseason   Meal_Plan   rate
Double  Deluxe/Ac   1200    NULL    NULL    CP          0
Double  Deluxe/Ac   1200    NULL    NULL    MAP         500
Double  Deluxe/Ac   1200    NULL    NULL    EP          200
single  Deluxe/Ac   1200    NULL    NULL    CP          0
single  Deluxe/Ac   1200    NULL    NULL    MAP         500
single  Deluxe/Ac   1200    NULL    NULL    EP          200
Double  Deluxe/Ac   1200    NULL    NULL    CP          0
Double  Deluxe/Ac   1200    NULL    NULL    MAP         600
Double  Deluxe/Ac   1200    NULL    NULL    CP          400
Double  Deluxe/Ac   1200    NULL    NULL    EP          400
Double  Deluxe/Ac   1200    NULL    NULL    MAP         600 ........

我的结果显示 48 行...我缺少什么?

4

2 回答 2

2

您的联接向您显示所有匹配的行:

r.Hotel_ID = '1' 
and r.Name = 'Deluxe/Ac' 
and o.Name = 'Double' 
and m.Meal_Plan = 'MAP' 
and '2013-09-09' between r.startseason and r.endseason 

或者

r.startseason is null 
and r.endseason is null 

您可能想要执行以下操作:

WHERE r.Hotel_ID = '1' 
AND r.Name = 'Deluxe/Ac' 
AND o.Name = 'Double' 
AND m.Meal_Plan = 'MAP' 
AND ('2013-09-09' between r.startseason and r.endseason 
    OR (r.startseason is null 
    AND r.endseason is null))

如果有季节价格,这仍然会始终带回淡季价格,但您也可以添加一些排序并仅使用第一行(等等)

于 2013-09-23T07:51:26.357 回答
0

您可以尝试以下方法:

select o.Name,r.Name,r.Rate,r.startseason, r.endseason, m.Meal_Plan,

rate =CASE WHEN r.startseason =NULL THEN NULL WHEN r.endseason =NULL THEN NULLL ELSE m.rate END from OccupancyType o inner join RoomType r on o.Hotel_ID = r.Hotel_ID and r.Name = 'Deluxe/Ac' and o.Name = 'Double' 在 m.Hotel_ID = o.Hotel_ID 和 m.Meal_Plan = 'MAP' 上内连接 Hotel_MealPlan m,其中 r.Hotel_ID = '1' 和 (('2013-09-09' 在 r.startseason 和r.endseason) 或 r.startseason 为 null 或 r.endseason 为 null )

于 2013-09-23T08:03:08.987 回答