Click here to Skip to main content
15,867,568 members
Articles / Database Development / SQL Server
Tip/Trick

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 56K   5   11
Calculate running total or cumulative sum from a table in a SQL Server database.
SQL
SELECT Field1, (SELECT SUM(Field1) FROM [Table] WHERE Field1 <= t.Field1) AS CumulativeSum
 FROM [Table] AS t

License

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


Written By
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
fordc0331-Aug-11 5:12 
GeneralReason for my vote of 2 This is a triangular join and is ver... Pin
fordc0329-Aug-11 11:28
fordc0329-Aug-11 11:28 
GeneralRe: How about a solution then from your end to justify the vote ... Pin
Mukit, Ataul30-Aug-11 20:50
Mukit, 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
fordc0331-Aug-11 5:27 
GeneralThe problem with this query is that you're assuming Field1 w... Pin
bjreed22-Feb-11 5:05
bjreed22-Feb-11 5:05 
GeneralRe: You have to have an increasing id field johnharold.. You can... Pin
Mukit, Ataul22-Feb-11 17:51
Mukit, 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
fordc0331-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
Mukit, Ataul22-Feb-11 2:58 
not working well meaning.. it's slow? or it doesn't work at all ????

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.