在SQL中运行Total

时间:2016-10-23 13:37:32

标签: sql-server

我有一个与SQl中的Running Total相关的查询。我尝试计算运行总计并将数据放在临时表中。然后获取数据,但它显示多个月而不是聚合数据

use tempdb

SELECT
  CONVERT(VARCHAR(7),PGLD,126) AS PMONTH,
    CLN,
    DIN,
    MAN,
    SUM(ALID) OVER (ORDER BY PGLD ROWS UNBOUNDED PRECEDING) AS CUMULATIVE_ALID,
    ALID
    into #1
FROM tempdb.dbo."AT"
GROUP BY
    CONVERT(VARCHAR(7),PGLD,126),
    PGLD,
    CLN,
    DIN,
    MAN,
    ALID;

SELECT PMONTH, CUMULATIVE_ALID FROM #1``

请帮助如何获取数据

1 个答案:

答案 0 :(得分:0)

PGLD上不需要GROUP BY。

use tempdb

SELECT
  CONVERT(VARCHAR(7),PGLD,126) AS PMONTH,
    CLN,
    DIN,
    MAN,
    SUM(ALID) OVER (ORDER BY PGLD ROWS UNBOUNDED PRECEDING) AS CUMULATIVE_ALID,
    ALID
    into #1
FROM tempdb.dbo."AT"
GROUP BY
    CONVERT(VARCHAR(7),PGLD,126),
    PGLD,
    CLN,
    DIN,
    MAN,
    ALID;

SELECT PMONTH, CUMULATIVE_ALID FROM #1