问题
如何根据航班日期计算每天的费用(成本酒店+成本航班)?
我需要建立数据库设计关系来计算埃及游客的每次旅行费用。 根据航班日期每人每天的飞行费用+酒店费用(客房费用+服务费)
假设我在阿姆斯特丹到埃及的航班日期为2017年6月23日
2017年6月26日从开罗到阿姆斯特丹将返回
所以计算如下:
Table tour
1 Tour 1
2 Tour 2
3 Tour 3
Table Tour Duration
Tour 1 3 days
Tour 2 6 days
表格旅行持续时间详情
Day flight Hotel
Day 1 Amsterdam to Cairo Hilton
Day 2 Cairo to Aswan Hilton
Day 3 Cairo to Amsterdam
从2017年6月23日至2017年6月26日
Table cost Per day
Day1 500 25
Day2 300 25
Day3 500
Table fligh data
Tour1 23/06/2017 26/06/2017 1350
Tour2 01/07/2017 07/07/2017 1425 (5 * 25 + 1300)
25表示希尔顿酒店每晚的住宿费用。 500表示从开罗阿姆斯特丹,阿姆斯特丹到开罗的成本航班。
300表示从开罗到阿斯旺的每次航班的费用。
如何根据航班日期设计表格关系以计算每天的费用(成本酒店+成本航班)?
**sql script**
SE [NileTravel]
GO
/****** Object: Table [dbo].[ActivityType] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ActivityType](
[ActivityTypeID] [int] NOT NULL,
[ActivityType] [nvarchar](50) NULL,
[ProductID] [int] NULL,
CONSTRAINT [PK_ActivityType] PRIMARY KEY CLUSTERED
(
[ActivityTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[AirLine] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AirLine](
[AirLineID] [int] NOT NULL,
[AirLineName] [nvarchar](50) NULL,
CONSTRAINT [PK_AirLine] PRIMARY KEY CLUSTERED
(
[AirLineID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[ChildrenDiscount] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ChildrenDiscount](
[ChildrenDiscountID] [int] NOT NULL,
[ProductID] [int] NULL,
[MinAge] [int] NULL,
[MaxAge] [int] NULL,
[Discount] [money] NULL,
CONSTRAINT [PK_ChildrenDiscount] PRIMARY KEY CLUSTERED
(
[ChildrenDiscountID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Destination] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Destination](
[BestimID] [int] NOT NULL,
[BestimName] [nvarchar](50) NULL,
[ParentID] [int] NULL,
[Image] [varbinary](max) NULL,
CONSTRAINT [PK_Bestiming] PRIMARY KEY CLUSTERED
(
[BestimID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[DurationDetails] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DurationDetails](
[DetailsDurationID] [int] NOT NULL,
[PackageDurationsID] [int] NULL,
[Days] [nvarchar](50) NULL,
[Flight] [nvarchar](50) NULL,
[Accomodate] [nvarchar](50) NULL,
[Excursion] [nvarchar](50) NULL,
[Transfer] [nvarchar](50) NULL,
CONSTRAINT [PK_DurationDetails] PRIMARY KEY CLUSTERED
(
[DetailsDurationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[FlightData] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FlightData](
[FlighID] [nvarchar](50) NOT NULL,
[FlightNo] [nvarchar](50) NOT NULL,
[FlightDate] [datetime] NULL,
[FlightTypeID] [int] NULL,
[AirLineID] [int] NULL,
[Arrival] [time](7) NULL,
[Departure] [time](7) NULL,
[AdultPrice] [money] NULL,
[ChildPrice] [money] NULL,
[Stock] [int] NULL,
[TotalPrice] [money] NULL,
[Active] [bit] NULL,
CONSTRAINT [PK_FlightData_1] PRIMARY KEY CLUSTERED
(
[FlighID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[FlightRoute] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FlightRoute](
[FlightTypeID] [int] NOT NULL,
[FlightFrom] [nvarchar](50) NULL,
[FlightTo] [nvarchar](50) NULL,
[Active] [bit] NULL,
CONSTRAINT [PK_FlightType] PRIMARY KEY CLUSTERED
(
[FlightTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Hotel] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Hotel](
[ProductID] [int] NOT NULL,
[ProductName] [nvarchar](50) NULL,
[BestimmingID] [int] NULL,
[TypeID] [int] NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[HotelPrice] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[HotelPrice](
[ProductPriceID] [nchar](10) NOT NULL,
[ProductID] [int] NULL,
[FromDate] [datetime] NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_ProductPrice] PRIMARY KEY CLUSTERED
(
[ProductPriceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Luggage] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Luggage](
[LuggageID] [int] NOT NULL,
[AirLineID] [int] NULL,
[KG] [float] NULL,
[CostPrice] [int] NULL,
[SalesPrice] [int] NULL,
[LuggageName] [nvarchar](50) NULL,
CONSTRAINT [PK_Luggage] PRIMARY KEY CLUSTERED
(
[LuggageID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Parent] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Parent](
[ParentID] [int] NOT NULL,
[Subject] [nvarchar](50) NULL,
[Text] [nvarchar](max) NULL,
[Slug] [nvarchar](50) NULL,
CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED
(
[ParentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[RoomOccupation] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RoomOccupation](
[RoomOccupationID] [int] NOT NULL,
[OccupationNo] [int] NULL,
[MaxAdult] [int] NULL,
[MaxOccupation] [int] NULL,
[Price] [money] NULL,
[ProductID] [int] NULL,
CONSTRAINT [PK_RoomOccupation] PRIMARY KEY CLUSTERED
(
[RoomOccupationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[RoomService] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RoomService](
[RoomServiceID] [int] NOT NULL,
[RoomService] [nvarchar](50) NULL,
[ProductID] [int] NULL,
CONSTRAINT [PK_RoomService] PRIMARY KEY CLUSTERED
(
[RoomServiceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[RoomType] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RoomType](
[RoomTypeID] [int] NOT NULL,
[RoomType] [nvarchar](50) NULL,
[ProductID] [int] NULL,
[RoomPrice] [money] NULL,
CONSTRAINT [PK_RoomType] PRIMARY KEY CLUSTERED
(
[RoomTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Services] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Services](
[ServiceID] [int] NOT NULL,
[ServiceType] [nvarchar](50) NULL,
[ProductID] [int] NULL,
[ServicePrice] [money] NULL,
CONSTRAINT [PK_Services] PRIMARY KEY CLUSTERED
(
[ServiceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Tour] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Tour](
[PackageID] [int] NOT NULL,
[PackageName] [nvarchar](100) NULL,
[BestimID] [int] NULL,
[Duration] [nvarchar](50) NULL,
CONSTRAINT [PK_Package] PRIMARY KEY CLUSTERED
(
[PackageID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[TourDestination] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TourDestination](
[PackageID] [int] NOT NULL,
[BestomingID] [int] NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[TourDuration] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TourDuration](
[PackageDurationsID] [int] NOT NULL,
[PackageID] [int] NULL,
[PackageDuration] [int] NULL,
[NightCounts] [int] NULL,
CONSTRAINT [PK_PackageDuration] PRIMARY KEY CLUSTERED
(
[PackageDurationsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[TourType] Script Date: 23/06/2017 1:21:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TourType](
[PackageTypeID] [int] NOT NULL,
[PackageType] [nvarchar](50) NULL,
CONSTRAINT [PK_PackageType] PRIMARY KEY CLUSTERED
(
[PackageTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ActivityType] WITH CHECK ADD CONSTRAINT [FK_ActivityType_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[ActivityType] CHECK CONSTRAINT [FK_ActivityType_Product]
GO
ALTER TABLE [dbo].[ChildrenDiscount] WITH CHECK ADD CONSTRAINT [FK_ChildrenDiscount_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[ChildrenDiscount] CHECK CONSTRAINT [FK_ChildrenDiscount_Product]
GO
ALTER TABLE [dbo].[DurationDetails] WITH CHECK ADD CONSTRAINT [FK_DurationDetails_PackageDuration] FOREIGN KEY([PackageDurationsID])
REFERENCES [dbo].[TourDuration] ([PackageDurationsID])
GO
ALTER TABLE [dbo].[DurationDetails] CHECK CONSTRAINT [FK_DurationDetails_PackageDuration]
GO
ALTER TABLE [dbo].[FlightData] WITH CHECK ADD CONSTRAINT [FK_FlightData_AirLine] FOREIGN KEY([AirLineID])
REFERENCES [dbo].[AirLine] ([AirLineID])
GO
ALTER TABLE [dbo].[FlightData] CHECK CONSTRAINT [FK_FlightData_AirLine]
GO
ALTER TABLE [dbo].[HotelPrice] WITH CHECK ADD CONSTRAINT [FK_ProductPrice_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[HotelPrice] CHECK CONSTRAINT [FK_ProductPrice_Product]
GO
ALTER TABLE [dbo].[Luggage] WITH CHECK ADD CONSTRAINT [FK_Luggage_AirLine] FOREIGN KEY([AirLineID])
REFERENCES [dbo].[AirLine] ([AirLineID])
GO
ALTER TABLE [dbo].[Luggage] CHECK CONSTRAINT [FK_Luggage_AirLine]
GO
ALTER TABLE [dbo].[RoomOccupation] WITH CHECK ADD CONSTRAINT [FK_RoomOccupation_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[RoomOccupation] CHECK CONSTRAINT [FK_RoomOccupation_Product]
GO
ALTER TABLE [dbo].[RoomService] WITH CHECK ADD CONSTRAINT [FK_RoomService_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[RoomService] CHECK CONSTRAINT [FK_RoomService_Product]
GO
ALTER TABLE [dbo].[RoomType] WITH CHECK ADD CONSTRAINT [FK_RoomType_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Hotel] ([ProductID])
GO
ALTER TABLE [dbo].[RoomType] CHECK CONSTRAINT [FK_RoomType_Product]
GO
ALTER TABLE [dbo].[TourDestination] WITH CHECK ADD CONSTRAINT [FK_PackageBestim_Bestiming] FOREIGN KEY([BestomingID])
REFERENCES [dbo].[Destination] ([BestimID])
GO
ALTER TABLE [dbo].[TourDestination] CHECK CONSTRAINT [FK_PackageBestim_Bestiming]
GO
ALTER TABLE [dbo].[TourDestination] WITH CHECK ADD CONSTRAINT [FK_PackageBestim_Package] FOREIGN KEY([PackageID])
REFERENCES [dbo].[Tour] ([PackageID])
GO
ALTER TABLE [dbo].[TourDestination] CHECK CONSTRAINT [FK_PackageBestim_Package]
GO
ALTER TABLE [dbo].[TourDuration] WITH CHECK ADD CONSTRAINT [FK_PackageDuration_Package] FOREIGN KEY([PackageID])
REFERENCES [dbo].[Tour] ([PackageID])
GO
ALTER TABLE [dbo].[TourDuration] CHECK CONSTRAINT [FK_PackageDuration_Package]
GO
更新原始帖子 你能在下面看到这张图片吗? 这是基于固定程序计算的显示
答案 0 :(得分:0)
游览时间不需要表格。首先,您可以将天数放在tour
表中,但即使这样也不错,因为表Tour Duration Details
中的记录已经隐含了天数。
价格随时间变化是另一回事。一个可能的模型是:
现在让我们说有人想在2017-08-25开始预订#1之旅。计算成本的查询将是:
select coalesce(sum(fp.price), 0) + coalesce(sum(hp.price), 0)
from tour_days d
left join flight f on f.flight_id = d.flight_id
left join flight_price fp on fp.flight_id = f.flight_id
and date '2017-08-25' between fp.from_date and fp.to_date
left join hotel h on h.hotel_id = d.hotel_id
left join hotel_price hp on hp.hotel_id = h.hotel_id
and date '2017-08-25' between hp.from_date and hp.to_date
where d.tour_no = 1;