Running aggregations is the aggregation of values over time.
In this example, we assume that there is a ordertotalbyyear view that shows the total number of orders by year and looks at the contents:
The select orderyear,qtyfrom ordertotalbyyear output is as follows order qty2007 254892008 162472006 9581 assumes that it is necessary to return the annual order year, the quantity, and the total annual operating volume, that is, each year, the sum of the number of previous years is calculated. So, the record of the earliest year 2006 in the view, the total running amount is equal to the quantity of year, the second year 2007, the running total is the first year plus the second year's sum, and so on. to complete this task, you can return an annual order year and quantity by querying an instance of the view, and then, Use a correlated subquery for the second instance of the query to calculate the run problem, which filters less than or equal to all annual, for the current year and sums the quantity: Select orderyear,qty, (Select sum (02.qty) from ordertotalbyyear as 02 where 02.orderyear<=01.orderyear) as runqtyfrom ordertotalyear as 01order by orderyear This query returns the following output:orderyear qty runqty2006 9581 95812007 25489 350702008 16237 51317
This article is from the "Growth Cornerstone" blog, be sure to keep this source http://jeffxi.blog.51cto.com/8540759/1641120
T-SQL uses run aggregations