2016-10-23 140 views
-1

我有一個查詢與SQl中運行總計相關。我嘗試計算運行總數並將數據放入臨時表中。然後,源數據,但它顯示了多個月沒有聚合一個在SQL中運行總計

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`` 

請幫助如何讓您不必在PGLD GROUP BY數據

+0

您可以添加示例數據和預期結果 –

+0

您正在使用哪個版本的SQL Server? –

+0

@TT。肯定是'2012 +' –

回答

0

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 
+0

感謝主席的回覆..如果我在Group by子句中刪除PGLD,它將顯示錯誤。 – Deepanshu

+0

消息8120,級別16,狀態1,行8 列'tempdb.dbo.AT.PGLD'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。 – Deepanshu

+0

例如數據集的: – Deepanshu