2016-09-07 52 views
2

我怎樣才能得到一個跑分的總金額在SQL 2014組?如何獲取在SQL Server中使用組運行小計

我有交易量的表。我需要總結一下,爲每個有數據的項目和季度設置一行,並且需要在每個項目中運行一個小計。每個新項目的運行總數都需要重置爲零。

這是我到目前爲止有:

SELECT [ProjectId] 
    , SUM(ActualAmount) AS PeriodAmount 
    , SUM(ActualAmount) OVER (PARTITION BY ProjectId ORDER BY ProjectId,YearQuarter) 
     AS FairMarketValue 

    FROM GLSnapshot 
    GROUP BY [ProjectId] , [YearQuarter] 

目前,我得到這個錯誤:

消息8120,級別16,狀態1,3號線

Column 'GLSnapshot.ActualAmount' is invalid in the 
select list because it is not contained in either an 
aggregate function or the GROUP BY clause. 

樣本數據:假設我有一個表GLSnapshot以下數據:

ProjectId, YearQuarter, ActualAmount 
'A', '2015Q1' , 9000.00 
'A', '2015Q1' , 100.00 
'A', '2015Q2' , 50.00 
'A', '2015Q3' , 50.00 
'A', '2015Q3' , 200.00 
'B', '2015Q1' ,80000.00 

我應該得到

ProjectId, YearQuarter, PeriodAmount, FairMarketValue (Running Subtotal): 
'A', '2015Q1' , 9100.00 , 9100.00 
'A', '2015Q2' , 50.00 , 9150.00 
'A', '2015Q3' , 250.00 , 9400.00 
'B', '2015Q1' ,80000.00 , 80000.00 
+0

這將是有益的,如果你能包括樣本數據以及預期結果集。 –

+1

https://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx –

+0

不,如果你註釋掉你fairMarketValue計算的工作?另外,我認爲你必須將你的'yearQuarter'值分成單獨的'Year'和'Quarter'字段。 – Beth

回答

2

OLAP功能後計算聚集,則不能使用ActualAmount,必須SUM(ActualAmount)。並且無需通過ProjectId進行訂購,因爲它已經在PARTITION BY中。最後使用ROWS UNBOUNDED PRECEDING否則默認爲RANGE UNBOUNDED PRECEDING這是比較昂貴的,並且可能不返回預期的結果:

SELECT [ProjectId] 
    , [YearQuarter] 
    , SUM(ActualAmount) AS PeriodAmount 
    , SUM(SUM(ActualAmount)) 
     OVER (PARTITION BY ProjectId 
      ORDER BY YearQuarter 
      ROWS UNBOUNDED PRECEDING) AS FairMarketValue 

    FROM GLSnapshot 
    GROUP BY [ProjectId] , [YearQuarter] 
+0

阿哈 - 偉大的作品!非常感謝。 –

0

您可以使用行無界的前面,這將給運行總

;with cte as (
select ProjectID, YearQuarter, ActualAmount 
from GLSnapshot 
) , cte2 as (
select ProjectID, YearQuarter, sum(ActualAmount) SumActualAmount from cte Group by ProjectID, YearQuarter 
) Select *, sum(SumActualAmount) over(partition by projectid order by projectid, yearquarter rows unbounded preceding) as RunningTotal from cte2 
+0

我得到同樣的錯誤如上:列,因爲它不是在聚合函數或GROUP BY子句中包含「GLSnapshot.ActualAmount」在選擇列表中無效。 –

+0

更改查詢以根據您的輸入設置更正 –

0

試試這個結果如下:

CREATE TABLE #GLSnapshot (ProjectId VARCHAR(5), YearQuarter VARCHAR(6), ActualAmount NUMERIC(18,2)) 

INSERT INTO #GLSnapshot 

SELECT 'A', '2015Q1' , 9000.00 UNION ALL 
SELECT 'A', '2015Q1' , 100.00 UNION ALL 
SELECT 'A', '2015Q2' , 50.00 UNION ALL 
SELECT 'A', '2015Q3' , 50.00 UNION ALL 
SELECT 'A', '2015Q3' , 200.00 UNION ALL 
SELECT 'B', '2015Q1' ,80000.00 



;WITH T 
AS 
(
    SELECT ROW_NUMBER() over(partition by ProjectId ORDER by YearQuarter) RN, 
    ProjectId,YearQuarter,sum(ActualAmount) PeriodAmount 
    FROM #GLSnapshot 
    GROUP BY ProjectId,YearQuarter 
) 
SELECT T1.ProjectId,T1.YearQuarter,T1.PeriodAmount, SUM(T2.PeriodAmount) FairMarketValue 
FROM T T1 
INNER JOIN T T2 ON T1.ProjectId = T2.ProjectId and T1.RN >= T2.RN 
GROUP BY T1.ProjectId,T1.YearQuarter,T1.PeriodAmount 
0

可以使用ROWS無界嘗試此查詢前茬

;with cte as (
select ProjectID, YearQuarter, ActualAmount 
from GLSnapshot 
) , cte2 as (
select ProjectID, YearQuarter, sum(ActualAmount) SumActualAmount from cte Group by ProjectID, YearQuarter 
) Select *, sum(SumActualAmount) over(partition by projectid order by projectid, yearquarter rows unbounded preceding) as RunningTotal from cte2 
相關問題