Click here to Skip to main content
12,295,791 members (34,345 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.3K views
1 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 2 With Cube and RollUp is not meant fo... Pin
Anurag Gandhi28-Aug-11 21:16
memberAnurag 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.

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