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

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
于 2013-02-20T11:44:38.300 に答える