Monday, July 10, 2017

SQL Server -Calculate Running Total

Many times, you required to show information of each transaction and also keep a Running Totals 
here you go with example
CREATE TABLE CustomerOrders( OrderID int identity, Amount Decimal(8,2), OrderDate SmallDatetime default getdate() ) Go  INSERT INTO CustomerOrders(Amount) Values(120.12) INSERT INTO CustomerOrders(Amount) Values(20.12) INSERT INTO CustomerOrders(Amount) Values(10.12) INSERT INTO CustomerOrders(Amount) Values(30.12) INSERT INTO CustomerOrders(Amount) Values(40) GO
 Calculating Running Total

 select OrderID, OrderDate, CO.Amount
 ,(select sum(Amount) from CustomerOrders 
 where OrderID <= CO.OrderID)
 'Running Total'
from CustomerOrders CO 


No comments:

Search This Blog

DAX - Grouping on multiple columns and the count

Please go thorugh the below URL for entire soultion. http://community.powerbi.com/t5/Desktop/DAX-Grouping-on-multiple-columns-and-the-cou...