SQL使用SUM和OVER获取运行计数

时间:2017-06-26 17:33:08

标签: sql sql-server tsql case

在sql中,我为每个项目都有一个历史记录表,他们可以记录每个动作的数量。我正在尝试根据是否是一个out或in的活动来计算我们拥有的项目的数量。这是我的最终sql:

SELECT itemid,
       activitydate,
       activitycode,
       SUM(quantity) AS quantity,
       SUM(CASE WHEN activitycode = 'IN'
           THEN quantity
           WHEN activitycode = 'OUT'
           THEN -quantity
           ELSE 0 END) OVER (PARTITION BY itemid ORDER BY activitydate rows unbounded preceding) AS runningcount
FROM   itemhistory
GROUP BY itemid,
         activitydate,
         activitycode

这导致:

+--------+-------------------------+--------------+----------+--------------+
| itemid |      activitydate       | activitycode | quantity | runningcount |
+--------+-------------------------+--------------+----------+--------------+
|      1 | 2017-06-08 13:58:00.000 | IN           |        1 |            1 |
|      1 | 2017-06-08 16:02:00.000 | IN           |        6 |            2 |
|      1 | 2017-06-15 11:43:00.000 | OUT          |        3 |            1 |
|      1 | 2017-06-19 12:36:00.000 | IN           |        1 |            2 |
|      2 | 2017-06-08 13:50:00.000 | IN           |        5 |            1 |
|      2 | 2017-06-12 12:41:00.000 | IN           |        4 |            2 |
|      2 | 2017-06-15 11:38:00.000 | OUT          |        2 |            1 |
|      2 | 2017-06-20 12:54:00.000 | IN           |       15 |            2 |
|      2 | 2017-06-08 13:52:00.000 | IN           |        5 |            3 |
|      2 | 2017-06-12 13:09:00.000 | IN           |        1 |            4 |
|      2 | 2017-06-15 11:47:00.000 | OUT          |        1 |            3 |
|      2 | 2017-06-20 13:14:00.000 | IN           |        1 |            4 |
+--------+-------------------------+--------------+----------+--------------+

我希望最终结果如下:

+--------+-------------------------+--------------+----------+--------------+
| itemid |      activitydate       | activitycode | quantity | runningcount |
+--------+-------------------------+--------------+----------+--------------+
|      1 | 2017-06-08 13:58:00.000 | IN           |        1 |            1 |
|      1 | 2017-06-08 16:02:00.000 | IN           |        6 |            7 |
|      1 | 2017-06-15 11:43:00.000 | OUT          |        3 |            4 |
|      1 | 2017-06-19 12:36:00.000 | IN           |        1 |            5 |
|      2 | 2017-06-08 13:50:00.000 | IN           |        5 |            5 |
|      2 | 2017-06-12 12:41:00.000 | IN           |        4 |            9 |
|      2 | 2017-06-15 11:38:00.000 | OUT          |        2 |            7 |
|      2 | 2017-06-20 12:54:00.000 | IN           |       15 |           22 |
|      2 | 2017-06-08 13:52:00.000 | IN           |        5 |           27 |
|      2 | 2017-06-12 13:09:00.000 | IN           |        1 |           28 |
|      2 | 2017-06-15 11:47:00.000 | OUT          |        1 |           27 |
|      2 | 2017-06-20 13:14:00.000 | IN           |        1 |           28 |
+--------+-------------------------+--------------+----------+--------------+

1 个答案:

答案 0 :(得分:2)

您想要sum(sum()),因为这是一个聚合查询:

SELECT itemid, activitydate, activitycode,
       SUM(quantity) AS quantity,
       SUM(SUM(CASE WHEN activitycode = 'IN' THEN quantity
                    WHEN activitycode = 'OUT' THEN -quantity
                    ELSE 0
               END)
           ) OVER (PARTITION BY itemid ORDER BY activitydate ) AS runningcount
FROM itemhistory
GROUP BY itemid, activitydate, activitycode