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

しかし、開始日の前に以前のデータがあり、それらの初期データを開始日の値に追加したい場合はどうでしょうか。例えば; 最初から 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 に答える