2

假设我们有一张桌子:

SELECT SUM(A) AS TOTALS,DATE,STUFF FROM TABLE WHERE DATE BETWEEN 'DATESTART' AND 'DATEEND'
GROUP BY DATE,STUFF

通常这得到总数为:

totals stuff    date
23      x      01.01.1900
3       x      02.01.1900
44      x      06.01.1900

但是,如果我们在 startdate 之前有之前的数据,我想将这些初始数据添加到我的 startdate 值中;例如; 从一开始,我已经有了 x 的总和值,可以说 100,所以我希望我的表从 123 开始并添加以前的数据,例如:123 126 126+44 等等......

totals stuff    date
123      x      01.01.1900
126      x      02.01.1900
170      x      06.01.1900

我怎样才能做到这一点?

源数据:

WITH    Stocks
          AS (
               SELECT
                Dep.Dept_No ,
                SUM(DSL.Metre) AS Metre ,
                CONVERT(VARCHAR(10), Date, 112) AS Date
               FROM
                DS (NOLOCK) DSL
                JOIN TBL_Depts (NOLOCK) Dep ON Dep.Dept_No = DSL.Dept
               WHERE
                1 = 1 AND
                DSL.Sil = 0 AND
                DSL.Depo IN ( 5000, 5001, 5002, 5003, 5004, 5014, 5018, 5021, 5101, 5109, 5303 ) AND
                Dep.Dept_No NOT IN ( 6002 ) AND
                Dep.Dept_No IN ( 6000, 6001, 6003, 6004, 6005, 6011, 6024, 6030 ) AND
                DSL.Date BETWEEN '2013-06-19' AND '2013-06-20'
               GROUP BY
                Dep.Dept_No ,
                CONVERT(VARCHAR(10), Date, 112)
             )
    SELECT
        Stocks.Metre ,
        Dep.Dept AS Dept ,
        Stocks.Date
    FROM
        Stocks
        LEFT JOIN TBL_Depts (NOLOCK) Dep ON Stocks.Dept = Dep.Dept
    ORDER BY
        Stocks.Metre DESC
4

2 回答 2

2

任何具有窗口和分析功能的 RDBMS(SQL Server 2012、PostgreSQL 但不是 MySQL)

SELECT
    SumA + SUM(SumARange) OVER (ORDER BY aDate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS TOTALS,
    other, aDate
FROM
    (
    SELECT
        SUM(a) AS SumARange,
        other, aDate
    FROM
        SomeTable
    WHERE
        aDate BETWEEN '20130101' AND '20130106'
    GROUP BY
        other, aDate
    ) X
    CROSS JOIN
    (
    SELECT
        SUM(a) AS SumA
    FROM
        SomeTable
    WHERE
        aDate < '20130101'
    ) Y
ORDER BY
    aDate;

或者

SELECT
    SUM(SumA) OVER () + SUM(SumARange) OVER (ORDER BY aDate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS TOTALS,
    other, aDate
FROM
    (
    SELECT
        SUM(CASE WHEN aDate < '20130101' THEN a ELSE 0 END) AS SumA,
        SUM(CASE WHEN aDate BETWEEN '20130101' AND '20130106' THEN a ELSE 0 END) AS SumARange,
        other, aDate
    FROM
        SomeTable
    WHERE
        aDate <= '20130106'
    GROUP BY
        other, aDate
    ) X
ORDER BY
    aDate;

SQLFiddle 示例另一个

于 2013-06-20T12:59:16.803 回答
0

将选项与 APPLY 运算符一起使用来计算总数。您还需要在 GROUP BY 子句中添加额外的 CASE 表达式

;WITH cte AS
(
 SELECT SUM(a) AS sumA, [stuff], MAX([Date]) AS [Date]
 FROM SomeTable
 WHERE [Date] <= '20130106'
 GROUP BY [stuff], CASE WHEN [Date] <= '20130101' THEN 1 ELSE [Date] END
 )
 SELECT o.total, [stuff], [Date]
 FROM cte c CROSS APPLY (
                         SELECT SUM(c2.sumA) AS total
                         FROM cte c2
                         WHERE c.[Date] >= c2.[Date]
                         ) o

请参阅SQLFiddle上的示例

于 2013-06-22T22:21:49.450 回答