0

Hi all I have a task where I have to calculate a price across multiple date ranges and I am stuck on how I would do it.

I have query that returns a set of date ranges and a price for each rage the ranges are sequential and are UK dates & currency

FromDate     ToDate         PricePerDay
01/05/2013   06/05/2013     £7
07/05/2013   20/05/2013     £12

The query has 2 parameters: @date - which is a day the client wants training. @NumberOfDays - which is the number of days.

Now if the client submits the @date = 02/05/2013 and number of NumberOfDays = 2 the query will only return the first line and its fairly easy to read the data back and say ok the total price will be 2x£7.

But if the client submits @date = 04/05/2013 and number of NumberOfDays = 7 then it will return both lines above and I will have to calculate as follows

3 Days @ £7 and 4 days @ £12 because the date the client selected crosses both ranges.

I have to do this in a VB.net class (business Logic Class) company policy that DB is storage only and should not define business rules.

Any help or tips would be appreciated on this.

Jason

4

1 回答 1

2

这是 SQL (SQL Server) 中的答案

为了方便使用sqlfiddle,我将其编写为存储过程。您可以将 SQL 嵌入到 VB.Net 类中。

它通过挑选指定日期范围内的行来工作(我认为您已经弄清楚了这一点)。然后,如果需要,它会截断每个范围以适合指定范围。最后计算出每个截断范围中有多少天,乘以该范围的成本,然后将它们全部相加。

Create Table Rates (
  FromDate datetime,
  ToDate datetime,
  PricePerDay money
);

Insert Into Rates (FromDate, ToDate, PricePerDay) Values
  ('2013-05-01', '2013-05-06', 7),
  ('2013-05-07', '2013-05-20', 12);

GO

Create Procedure Cost(@FromDate datetime, @Days int) As
  Declare @ToDate date
  Set @ToDate = DateAdd(Day, @Days, @FromDate)
  Select 
    Sum(DateDiff(Day, FromDate, ToDate) * PricePerDay) As TotalCost
  From (
    Select
      Case When @FromDate > FromDate Then @FromDate Else FromDate End As FromDate,
      Case When @ToDate < DateAdd(Day, 1, ToDate) Then @ToDate Else DateAdd(Day, 1, ToDate) End As ToDate,
      PricePerDay
    From
      Rates
    Where
      FromDate < @ToDate And
      ToDate > @FromDate
  ) X
GO

exec Cost '2013-05-02', 2
GO
exec Cost '2013-05-04', 7
于 2013-05-11T00:41:16.373 回答