我正在开发一种支持搜索和预订的酒店库存管理系统。我对该系统还很陌生,因此在编写查询以从清单中提取可用房间时需要帮助。
假设用户搜索包含以下值:
地区ID:1(例如,迪拜) 入住日期:31-12-2018和退房日期:01-01-2019(该日期应在room_rate_types表的valid_from和valid_to之内,并且不应位于room_rate_type_blackout_dates表的from_date和to_date内,并且不应位于room_rate_type_booked_dates表的from_date和to_date,如果它位于room_rate_type_booked_dates表中,则应从room_types表的可用性列中减去已预订的(room_rate_type_booked_dates表)房间
成人1,儿童2,婴儿0:应在rooms_occupancy表中
我的搜索取决于下表,括号中提供了它们的相应列:
属性(region_Id),房间,rooms_occupancy(成人,儿童和婴儿),room_rate_types(可用性,valid_from和valid_to),room_rate_type_blackout_dates(从From_date到To_Date),room_rate_type_booked_dates(预订ID,From_date和to_Date)
properties ( stores the general details of the property)
pk property_id(primary key)
region_id (id of the region)
property_name
rooms ( stores the general details of the rooms)
pk room_id(primary key)
fk rooms_occupancy_id (the combination of the occupancies associated with the room)
rooms_occupancy ( stores the general details of the combination of the
occupancies in a single room)
pk room_occupancy_id(primary key)
fk room_id(foreign key that references the room table)
adult,
child,
infant
room_rate_types (stores of the general details of the rates per room)
pk rate_id(primary key)
valid_from,
valid_to,
available_rooms
fk room_id
room_rate_type_blackout_dates (stores the black out dates per rate)
pk rate_blk_date_id (primary key)
from_date,
to_date,
fk rate_id (rate_id of the room_rate_types table)
room_rate_type_booked_dates (stores the booking info of the rate table)
pk booking_id (primary key)
from_date,
to_date,
fk rate_id (rate_id of the room_rate_types table)
booked_rooms
预期结果应如下所述:
PropertyId, rateId, roomId, availblerooms
1 1 1 5
答案 0 :(得分:0)
我已尝试根据您的描述创建您的方案。它可以工作,但是可能会导致您对我所做的事情和您想做的事情提出更多疑问:
设置:
Create Table properties
(
property_id Int,
region_id Int,
property_name VarChar(25),
room_id Int
)
Insert Into properties Values
(1,1,'property1',1)
Create Table rooms
(
room_id Int,
rooms_occupancy_id Int, --(the combination of the occupancies associated with the room)
)
Insert Into rooms Values
(1,1)
Create Table rooms_occupancy --( stores the general details of the combination of the occupancies in a single room)
(
room_occupancy_id Int,
adult Int,
child Int,
infant Int
)
Insert Into rooms_occupancy Values
(1,1,2,0)
Create Table room_rate_types --(stores of the general details of the rates per room)
(
rate_id Int,
valid_from Date,
valid_to Date,
available_rooms Int,
rate_blk_date_id Int,
booking_id Int
)
Insert Into room_rate_types Values
(1,'2018-12-31','2019-01-01',6,1,1)
Create Table room_rate_type_blackout_dates --(stores the black out dates per rate)
(
rate_blk_date_id Int,
from_date Date,
to_date Date,
rate_id Int
)
Insert Into room_rate_type_blackout_dates Values
(1,'2018-12-29','2018-12-30',1)
Create Table room_rate_type_booked_dates --(stores the booking info of the rate table)
(
booking_id Int IDENTITY(1,1) NOT NULL,
from_date Date,
to_date Date,
rate_id Int,
booked_rooms Int
)
程序:
Declare @From As Date = '2018-12-31'
Declare @To As Date = '2019-01-01'
Declare @property As Int = 1
Declare @Region As Int = 1
Declare @roomratetypeid As Int = 1
If Exists
(
SELECT
p.property_id,
p.room_id,
rate_id = (Select rate_id From room_rate_types Where rate_id = @roomratetypeid),
Availablerooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1
FROM
properties p INNER JOIN
rooms r ON p.room_id = r.room_id INNER JOIN
rooms_occupancy ro ON r.rooms_occupancy_id = ro.room_occupancy_id
Where
p.property_id = @property And
p.region_id = @Region And
( --Check if requested dates are available
@From >= (Select valid_from From room_rate_types Where rate_id = @roomratetypeid) And
@To <= (Select valid_to From room_rate_types Where rate_id = @roomratetypeid)
) And
Not Exists (Select booking_id From room_rate_type_booked_dates Where @From >= from_date And @To <= to_date) And
Not Exists (Select rate_blk_date_id From room_rate_type_blackout_dates Where @From >= from_date And @To <= to_date)
) --If NOT exists, create booking and display results and adjust the available rooms
Begin
SELECT
p.property_id,
p.room_id,
rate_id = (Select rate_id From room_rate_types Where rate_id = @roomratetypeid),
Availablerooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1
FROM
properties p INNER JOIN
rooms r ON p.room_id = r.room_id INNER JOIN
rooms_occupancy ro ON r.rooms_occupancy_id = ro.room_occupancy_id
Where
p.property_id = @property And
p.region_id = @Region And
( --Check if requested dates are available
@From >= (Select valid_from From room_rate_types Where rate_id = @roomratetypeid) And
@To <= (Select valid_to From room_rate_types Where rate_id = @roomratetypeid)
)
Update room_rate_types Set available_rooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1;
Insert Into room_rate_type_booked_dates Values (@From,@To,@roomratetypeid,1)
End
Else
Select 'Not Available';
结果:
property_id room_id rate_id Availablerooms
1 1 1 5