Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Query for running total in SQL Server

, 26 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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):

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

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)

Share

About the Author

S Douglas
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... PinmemberAnurag Gandhi28-Aug-11 22:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Aug 2011
Article Copyright 2011 by S Douglas
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid