从我的PitchValues表中提取正确的结果

时间:2013-02-21 13:08:54

标签: tsql

我有点沮丧,想知道你是否可以提供帮助:

我有一个Pitch Values Table,其中包含以下列PitchValues_Skey,PitchType_Skey(这是一个外键),Start Date,End Date和finally值:

例如:

1    7   01/01/2010    31/12/2010   £15
2    7   01/01/2011    31/12/2011   £20

我想要做的就是更新我的预订表,每张预订的数量是多少,所以我把下面的代码放在一起,当我只有2010年的数据时工作正常,但我知道有2011年和2012年,并且想要更新它,但它只会更新2010年的价格。

SELECT        Bookings.Booking_Skey, DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate) * PitchValues.Value AS BookingValue, 
                         PitchValues.PitchType_Skey
FROM            Bookings INNER JOIN
                         PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
WHERE        (Bookings.Booking_Skey = 1)

所以,当我运行上面的查询时,我希望看到一行数据,但我看到4(见下文)

我希望如此:

Booking_Skey    BookingValue    PitchType_Skey
      1               420                     4

但我得到了这个

Booking_Skey    BookingValue    PitchType_Skey
     1                 420                  4
     1                 453.6                4
     1                 476.7                4
     1                 476.7                4

1 个答案:

答案 0 :(得分:0)

现在全部排序,感谢您的帮助。

SELECT  Bookings.Booking_Skey, DATEDIFF(DAY, Bookings.ArrivalDate, Bookings.DepartureDate) * PitchValues.Value AS BookingValue, PitchValues.PitchType_Skey
FROM    Bookings
INNER JOIN PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
    AND Bookings.ArrivalDate BETWEEN PitchValues.StartDate AND PitchValues.EndDate
WHERE (Bookings.Booking_Skey = 1)