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
如果存在空值,您还可能希望在列周围执行 ISNULL(,0),整个值将变为空值。像这样:
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