T-SQL uses run aggregations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.