SQL使用特定条件从2列表中获取活动价格

时间:2018-03-28 10:52:00

标签: sql sql-server price

如何选择有效价格,首要任务是季节价格,如果是在日期范围内,则选择季节价格。但是如果它不在日期范围内,那么从另一个表中选择它(TBL_ROOM_TYPE)。

pic 1

 SELECT
  TBL_ROOM_TYPE.ID_ROOM_TYPE,
  COALESCE(TBL_SEASON_TYPE_MAP.SEASON_PRICE, TBL_ROOM_TYPE.TYPE_PRICE) AS price
FROM
  TBL_ROOM_TYPE 
  left JOIN TBL_SEASON_TYPE_MAP ON TBL_ROOM_TYPE.ID_ROOM_TYPE = TBL_SEASON_TYPE_MAP.ID_ROOM_TYPE
join  TBL_ROOM_SEASON on TBL_ROOM_SEASON.ID_SEASON=TBL_SEASON_TYPE_MAP.ID_SEASON
and (@ArrivalDate BETWEEN TBL_ROOM_SEASON.[START_DATE]  AND TBL_ROOM_SEASON.[END_DATE] )

所以我想将id 1和3中的价格更改为与pic 1相同pic2

TBL_SEASON_TYPE_MAP

TBL_ROOM_SEASON

TBL_ROOM_TYPE

1 个答案:

答案 0 :(得分:0)

请按照这些行和表格名称及其图像进行操作。

SELECT 
 R.ID_ROOM_TYPE
,CASE  
WHEN  START_DATE<'2018-03-18' AND END_DATE >'2018-03-18' THEN SEASON_PRICE 
ELSE TYPE_PRICE END AS PRICE
 FROM TBL_ROOM R 
LEFT JOIN TBL_ROOM_SEASON S ON S.ID_SEASON = R.ID_SEASON
LEFT JOIN TBL_ROOM_TYPE T ON T.ID_ROOM_TYPE = R.ID_ROOM_TYPE

Image for Reference

感谢。