Current Row + Sum of Previous Rows SQL Server


          In This Section, We See About How To Calculate Previous Row Sum value + Next Rows Values in SQL Server. We Sum Values One by One Orderly. 



Table


SQL

;WITH cte
AS
(
   SELECT ID,Prod,Value Val ,SUM(Value) Valsum
   FROM mData
   gROUP BY ID,Prod,Value
 
), cteRanked AS
(
   SELECT Valsum, ID,Prod, ROW_NUMBER() OVER(ORDER BY Id) rownum
   FROM cte
)

SELECT (SELECT SUM(Valsum) FROM cteRanked c2 WHERE c2.rownum <= c1.rownum )
AS Value,ID,Prod
FROM cteRanked c1;

Result:





Comments

Popular posts from this blog

Unable to perform operation on the item is locked in workspace

Insecure cookie setting: missing Secure flag

Display Canvasjs Line Chart using Data from Database