我应该知道这一点,但我不记得了,如何添加另一列来显示AmountDays * PitchValues.Value = results。
SELECT DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate) AS AmountDays,
PitchValues.Value
FROM Bookings
INNER JOIN PitchValues
ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
迄今为止的结果
AmountDays PitchValues.Value BookingValue
15 £15
7 £9
8 £25
答案 0 :(得分:1)
SELECT
DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate) AS AmountDays,
PitchValues.Value,
(DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate)) * PitchValues.Value AS YourCol
FROM
Bookings INNER JOIN
PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
或者,只是为了让它看起来漂亮:
SELECT
P0.AmountDays,
P0.Value,
(P0.AmountDays * P0.Value) as YourCol
FROM
(
SELECT
DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate) AS AmountDays,
PitchValues.Value
FROM
Bookings INNER JOIN
PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
) P0
如果存在null,您也可能希望在列周围执行ISNULL(,0),整个值将变为null。像这样:
SELECT
P0.AmountDays,
P0.Value,
(P0.AmountDays * P0.Value) as YourCol
FROM
(
SELECT
ISNULL(DATEDIFF(day, Bookings.ArrivalDate, Bookings.DepartureDate), 0) AS AmountDays,
ISNULL(PitchValues.Value, 0) as Value
FROM
Bookings INNER JOIN
PitchValues ON Bookings.PitchType_Skey = PitchValues.PitchType_Skey
) P0