1

希望您能提供帮助,我需要总计 TotalCapacity 列,以便根据 Where 语句给我一年的总计。我还需要看到彼此相邻的不同年份。谢谢

SELECT
    Capacity.StartDate,
    Capacity.EndDate,
    DATEDIFF(d, Capacity.StartDate, Capacity.EndDate) + 1 AS DaysOpen,
    Capacity.Capacity,
    (DATEDIFF(d, Capacity.StartDate, Capacity.EndDate) + 1) * Capacity.Capacity AS TotalCapacity,
FROM
    Capacity
WHERE 
    Capacity.StartDate >= '01 Jan 2010' 
    AND Capacity.EndDate <= '31 Dec 2010'
4

1 回答 1

1

猜猜你需要一些这样的:

SELECT SUM(TotalCapacity)
    FROM
    (
        SELECT
          Capacity.StartDate
          ,Capacity.EndDate
          ,DATEDIFF(d, Capacity.StartDate, Capacity.EndDate) + 1 AS DaysOpen
          ,Capacity.Capacity
          ,(DATEDIFF(d, Capacity.StartDate, Capacity.EndDate) + 1) * Capacity.Capacity AS TotalCapacity
        FROM
          Capacity
          WHERE Capacity.StartDate >= '01 Jan 2010' AND Capacity.EndDate <= '31 Dec 2010'
    ) t
于 2013-02-26T11:00:43.777 回答