总计多个Datediff列

时间:2013-02-26 10:36:00

标签: sql tsql

我希望你能提供帮助,我需要总计TotalCapacity列给我一个基于Where Statement的总年数。我还需要看到彼此不同的年份。感谢

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'

1 个答案:

答案 0 :(得分: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