65.9K
CodeProject is changing. Read more.
Home

Query for running total in SQL Server

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1 vote)

Aug 26, 2011

CPOL
viewsIcon

9861

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[^].