0

我应该知道这一点,但我不记得了,如何添加另一列来显示 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
4

1 回答 1

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

如果存在空值,您还可能希望在列周围执行 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
于 2013-02-20T11:44:38.300 回答