Click here to Skip to main content
15,391,989 members
Articles / Database Development / SQL Server
Posted 13 Feb 2011

Tagged as


5 bookmarked

Query for running total in SQL Server

Rate me:
Please Sign up or sign in to vote.
3.33/5 (6 votes)
31 Aug 2011CPOL
Calculate running total or cumulative sum from a table in a SQL Server database.
SELECT Field1, (SELECT SUM(Field1) FROM [Table] WHERE Field1 <= t.Field1) AS CumulativeSum
 FROM [Table] AS t


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


About the Author

Mukit, Ataul
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

GeneralReason for my vote of 3 I previously voted this a 2, but upo... Pin
fordc0331-Aug-11 5:12
Memberfordc0331-Aug-11 5:12 
GeneralReason for my vote of 2 This is a triangular join and is ver... Pin
fordc0329-Aug-11 11:28
Memberfordc0329-Aug-11 11:28 
GeneralRe: How about a solution then from your end to justify the vote ... Pin
Mukit, Ataul30-Aug-11 20:50
MemberMukit, Ataul30-Aug-11 20:50 
GeneralReason for my vote of 1 There are predicates just for this p... Pin
S Douglas25-Aug-11 13:23
professionalS Douglas25-Aug-11 13:23 
GeneralRe: Your vote is unfair. SQL 2000 doesn't have partition by. C... Pin
fordc0331-Aug-11 5:27
Memberfordc0331-Aug-11 5:27 
GeneralThe problem with this query is that you're assuming Field1 w... Pin
bjreed22-Feb-11 5:05
Memberbjreed22-Feb-11 5:05 
GeneralRe: You have to have an increasing id field johnharold.. You can... Pin
Mukit, Ataul22-Feb-11 17:51
MemberMukit, Ataul22-Feb-11 17:51 
Generalthe results is not matching with anything, i was thinking th... Pin
John Belalcazar22-Feb-11 3:14
professionalJohn Belalcazar22-Feb-11 3:14 
GeneralRe: The template he has written won't do that. You'd have to us... Pin
fordc0331-Aug-11 5:26
Memberfordc0331-Aug-11 5:26 
GeneralReason for my vote of 1 this is not working well Pin
John Belalcazar22-Feb-11 2:57
professionalJohn Belalcazar22-Feb-11 2:57 
GeneralRe: not working well meaning.. it's slow? or it doesn't work at ... Pin
Mukit, Ataul22-Feb-11 2:58
MemberMukit, Ataul22-Feb-11 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.