Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to add a "RunningSum" column to an existing table.
The table name = Rainbow_Lanes

Column names are Bowler, Date, Gm1 (Score) and Running Ttl
Used code below with these results....

Running Ttl or Sum(Gm1) results

Gm1    Running Ttl
173      346           2x Gm1
150      600           ????
153      306           2x Gm1
147      294
173      519
224      224


I tried to change formula in different ways but could not get running sum to work.
Used format from this link which worked until i tried to nest it into my code?

Calculating simple running totals in SQL Server[^]

What I have tried:

SQL
SELECT  Bowler,  Date,  Gm1, 
     (      SELECT SUM(Gm1)	FROM Rainbow_Lanes 
                  	WHERE Gm1 = o.Gm1	
                  	AND Date  <= o.Date
        		ORDER BY Date, Bowler
     )  AS  "Running Ttl"

FROM Rainbow_Lanes 
WHERE Bowler = "Ray McDonough"
ORDER BY Date

.........................
Posted
Updated 12-Feb-17 14:16pm
v2

1 solution

Try

SELECT Bowler, Date, Gm1,  
 ( SELECT SUM(Gm1) FROM Rainbow_Lanes L
 WHERE L.Bowler = R.Bowler 
 AND L.Date <= R.Date
 ) as [Running Total]

FROM Rainbow_Lanes R

where bowler = "Ray McDonough"

ORDER BY r.Date


The last where clause can be excluded to bring back all bowlers.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900