MW211 EXIT

devlog
MSSQL/累積列の作り方
2015年06月26日
[+表]と[-表]を統合して時系列で二列に並べて累積を逐一計算する場合は、
以下のような感じ
┌──────────────────────────────────────┐
│SELECT [±表].[日付],                                                       │
│       CASE [±表].[±区分]                                                 │
│         WHEN 1 THEN '+'                                                   │
│         WHEN 2 THEN '-'                                                   │
│       END AS [区分],                                                       │
│       [±表].[+値],                                                       │
│       [±表].[-値],                                                       │
│       ISNULL((                                                             │
│           SELECT SUM([+表].[値])                                          │
│               FROM [+表]                                                  │
│               WHERE [+表].[日付] < [±表].[日付]                          │
│                  OR  ([+表].[日付]   =  [±表].[日付]                     │
│                   AND [+表].[±区分] <= [±表].[±区分])                  │
│       ), 0) - ISNULL((                                                     │
│           SELECT SUM([-表].[値])                                          │
│               FROM [-表]                                                  │
│               WHERE [-表].[日付] < [±表].[日付]                          │
│                  OR  ([-表].[日付]   =  [±表].[日付]                     │
│                   AND [-表].[±区分] <= [±表].[±区分])                  │
│       ), 0) AS [累積]                                                      │
│    FROM (                                                                  │
│        SELECT [日付],                                                      │
│               1         AS [±区分],                                       │
│               SUM([値]) AS [+値],                                         │
│               NULL      AS [-値]                                          │
│            FROM [+表]                                                     │
│            GROUP BY [日付]                                                 │
│        UNION ALL                                                           │
│        SELECT [日付],                                                      │
│               2         AS [±区分],                                       │
│               NULL      AS [+値],                                         │
│               SUM([値]) AS [-値]                                          │
│            FROM [-表]                                                     │
│            GROUP BY [日付]                                                 │
│    ) AS [±表]                                                             │
│    ORDER BY [±表].[日付]   ASC,                                           │
│             [±表].[±区分] ASC;                                           │
└──────────────────────────────────────┘
[-表]がなければ、もっとシンプルになる。
┌──────────────────────────────────────┐
│SELECT [±表].[日付],                                                       │
│       [±表].[+値],                                                       │
│       ISNULL((                                                             │
│           SELECT SUM([+表].[値])                                          │
│               FROM [+表]                                                  │
│               WHERE [+表].[日付] <= [±表].[日付]                         │
│       ), 0) AS [累積]                                                      │
│    FROM (                                                                  │
│        SELECT [日付],                                                      │
│               SUM([値]) AS [+値]                                          │
│            FROM [+表]                                                     │
│            GROUP BY [日付]                                                 │
│    ) AS [±表]                                                             │
│    ORDER BY [±表].[日付] ASC;                                             │
└──────────────────────────────────────┘
分類:MSSQL