Click here to Skip to main content
15,885,366 members
Articles / Database Development / SQL Server
Alternative
Tip/Trick

Query for running total in SQL Server

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
26 Aug 2011CPOL 9.6K   1   1
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...

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):


SQL
SELECT
	FIELD_DESCRIPTION
	, SUM(FIELD_VALUE)
FROM
	tbl_Data
GROUP BY
	FIELD_DESCRIPTION
WITH CUBE

Or:


By Cube (the final set of FIELD_VALUE will have a total sum):


SQL
SELECT
	FIELD_DESCRIPTION
	, SUM(FIELD_VALUE)
FROM
	tbl_Data
GROUP BY
	FIELD_DESCRIPTION
WITH ROLLUP

Reference:
Summarizing Data Using ROLLUP[^].

License

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


Written By
Database Developer
United States United States
Just another hack, who manages to take care of issues that would otherwise go unsolved. Smile | :)

Comments and Discussions

 
GeneralReason for my vote of 2 With Cube and RollUp is not meant fo... Pin
Anurag Gandhi28-Aug-11 21:16
professionalAnurag Gandhi28-Aug-11 21:16 

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.