Use SQL to accumulate data

Source: Internet
Author: User

For example, if you have such a requirement, a table named User_Salary contains the UserName of each user)

Calculate a result set: the sum of the salaries and accumulated salaries of each person per month, as shown in the following table.

UserName Month Salary
AAA 2010/12 1000
AAA 2011/01 2000
AAA 2011/02 3000
BBB 2010/12 2000
BBB 2011/01 2500
BBB 2011/02 2500

Result

UserName Month Salary Cumulation
AAA 2010/12 1000 1000
AAA 2011/01 2000 3000
AAA 2011/02 3000 6000
BBB 2010/12 2000 2000
BBB 2011/01 2500 4500
BBB 2011/02 2500 7000

Of course, this result is well implemented in Excel, just a formula is needed:

Note that the G2 formula must remain unchanged in the first column, so it is $ F $2: F2, and then drag it down. However, this is only applicable when the data is fixed, if there are 100 employees, don't we need to drag them down. Of course, there may be other ways, so I don't know.

What can I do if I use SQL? The first thing that comes to mind is the cursor.

The cursor can indeed be implemented, and the write program can also be implemented, because their idea is the same: judge whether the name has been traversed. If it has been traversed, add up, if not, add it from 0. This is easy to understand, but it takes a lot of time to write. In fact, a SQL statement can be implemented, that is, subquery.

 
 
  1. create table User_Salary (UserName nvarchar(200), Month nvarchar(20), Salary int)     
  2.       
  3. go     
  4.       
  5. insert into User_Salary (UserName,Month,Salary ) values('AAA','2010/12',1000)     
  6. insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/01',2000)     
  7. insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/02',3000)     
  8. insert into User_Salary (UserName,Month,Salary ) values('BBB','2010/12',2000)     
  9. insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/01',2500)    
  10. insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/02',2500)    
  11.      
  12. go    
  13.      
  14. select UserName,Month,Salary,    
  15.     Cummulation=(    
  16.         select SUM(Salary)     
  17.         from     
  18.             User_Salary i    
  19.         where     
  20.             i.UserName=o.UserName and i.Month<=o.Month    
  21.         )    
  22. from User_Salary o    
  23. order by 1,2    
  24.      
  25. go    
  26.      
  27. drop table User_Salary 

As you know, the results of SQL queries are set-oriented, and such nested subqueries are the operations performed on each row before the entire result set is returned. Maybe this writing method is not easy to understand, so the following writing method should be easier to understand.

 
 
  1. create table User_Salary (UserName nvarchar(200), Month nvarchar(20), Salary int)    
  2.       
  3. go    
  4.    
  5. insert into User_Salary (UserName,Month,Salary ) values('AAA','2010/12',1000)    
  6. insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/01',2000)    
  7. insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/02',3000)    
  8. insert into User_Salary (UserName,Month,Salary ) values('BBB','2010/12',2000)    
  9. insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/01',2500)   
  10. insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/02',2500)   
  11.     
  12. go    
  13.      
  14. select    
  15.      A.UserName,A.Month,MAX(A.Salary) as Salary,SUM (B.Salary) as Accumulation    
  16. from   
  17.      User_Salary A inner join User_Salary B   
  18.  ON    
  19.      A.UserName = B.UserName   
  20.  where    
  21.      B.Month <= A.Month   
  22.  group by   
  23.      A.UserName,A.Month   
  24. order by       
  25.      A.UserName,A.Month   
  26.      
  27. go   
  28.      
  29. drop table User_Salary 

In this way, the Union method is better to understand. In fact, this is to divide each row into a group of values smaller than its month, and then perform operations. If this is not clear, the following SQL statement makes the query more understandable:

 
 
  1. select     
  2.    A.*,B.*    
  3. from     
  4.    User_Salary A inner join User_Salary B    
  5. ON     
  6.    A.UserName = B.UserName     
  7. where     
  8.    B.Month <= A.Month     
  9. order by 1,2 

The result is obvious.

 

This is the recursion related to subqueries. Can this be said ?) Algorithm.

Subquery execution plan

Join execution plan

Though the two execution plans mentioned above are different, they are generally consistent, I don't quite understand the difference. (I don't know whether to sort first and then join first or sort first, but I personally think the second one is easier to understand .).

Source: http://www.cnblogs.com/mylhei/archive/2011/03/09/1978184.html

  1. MySQL tips: optimize Limit with relevant parameters
  2. Six data migration methods for SQL Server databases
  3. MySQL database optimization) standalone MySQL database Optimization
  4. MySQL database optimization) MySQL database high-availability architecture solution
  5. MySQL Database Security Solution

Related Article

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.