Click here to Skip to main content
11,637,415 members (61,164 online)
Click here to Skip to main content

Tagged as

Query for running total in SQL Server

, 4 Sep 2011 CPOL 3.1K 1
This will work on SQL 2000/2005/2008.Most cumulative aggregations have constraints such as a begin and end date or grouped by customers.Your solution is good for a single aggregation without much filtering. People will find that the query plans generated would be very similiar to that of...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

25 Aug 2011
iamalik
I guess this is more faster:SELECT T1.Field1, SUM(T2.Field1) FROM [Table] T1, [Table] T2WHERE T1.Field1 >= T2.Field1GROUP BY T1.Field1
24 Aug 2011
Elina Blank
Using window functions (partition by) will speed up the process:SELECT Val, SUM(Val) OVER (Partition BY [YourPartitionValue] Order BY [OrderColumn] as CumulativeSumFROM [Table]
31 Aug 2011
Mukit, Ataul
4 alternatives  
Calculate running total or cumulative sum from a table in a SQL Server database.
26 Aug 2011
S Douglas
Another way of getting totals, whether it be by group or set is to use the CUBE and ROLLUP predicates.By group (each grouping of FIELD_DESCRIPTION will have a total sum):SELECT FIELD_DESCRIPTION , SUM(FIELD_VALUE)FROM tbl_DataGROUP BY FIELD_DESCRIPTIONWITH CUBEOr:By Cube...

License

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

Share

About the Author

fordc03
United States United States
No Biography provided

| | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 4 Sep 2011
Article Copyright 2011 by fordc03
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid