Running total for Oracle:

SELECT somedate, somevalue,
SUM(somevalue) OVER(ORDER BY somedate 
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 
AS RunningTotal
FROM Table

from http://www.codeproject.com/Articles/300785/Calculating-simple-running-totals-in-SQL-Server

Introduction

One typical question is, how to calculate running totals in SQL Server. There are several ways of doing it and this article tries to explain a few of them.

Test environment

First we need a table for the data. To keep things simple, let's create a table with just an auto incremented id and a value field.

  1. --------------------------------------------------------------------
  2. -- table for test
  3. --------------------------------------------------------------------
  4. CREATE TABLE RunTotalTestData (
  5. id int not null identity(1,1) primary key,
  6. value int not null
  7. );

And populate it with some data:

  1. --------------------------------------------------------------------
  2. -- test data
  3. --------------------------------------------------------------------
  4. INSERT INTO RunTotalTestData (value) VALUES (1);
  5. INSERT INTO RunTotalTestData (value) VALUES (2);
  6. INSERT INTO RunTotalTestData (value) VALUES (4);
  7. INSERT INTO RunTotalTestData (value) VALUES (7);
  8. INSERT INTO RunTotalTestData (value) VALUES (9);
  9. INSERT INTO RunTotalTestData (value) VALUES (12);
  10. INSERT INTO RunTotalTestData (value) VALUES (13);
  11. INSERT INTO RunTotalTestData (value) VALUES (16);
  12. INSERT INTO RunTotalTestData (value) VALUES (22);
  13. INSERT INTO RunTotalTestData (value) VALUES (42);
  14. INSERT INTO RunTotalTestData (value) VALUES (57);
  15. INSERT INTO RunTotalTestData (value) VALUES (58);
  16. INSERT INTO RunTotalTestData (value) VALUES (59);
  17. INSERT INTO RunTotalTestData (value) VALUES (60);

The scenario is to fetch a running total when the data is ordered ascending by the id field.

Correlated scalar query

One very traditional way is to use a correlated scalar query to fetch the running total so far. The query could look like:

  1. --------------------------------------------------------------------
  2. -- correlated scalar
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, (SELECT SUM(b.value)
  5. FROM RunTotalTestData b
  6. WHERE b.id <= a.id)
  7. FROM RunTotalTestData a
  8. ORDER BY a.id;

When this is run, the results are:

  1. id value running total
  2. -- ----- -------------
  3. 1 1 1
  4. 2 2 3
  5. 3 4 7
  6. 4 7 14
  7. 5 9 23
  8. 6 12 35
  9. 7 13 48
  10. 8 16 64
  11. 9 22 86
  12. 10 42 128
  13. 11 57 185
  14. 12 58 243
  15. 13 59 302
  16. 14 60 362

So there it was. Along with the actual row values, we have a running total. The scalar query simply fetches the sum of the value field from the rows where the ID is equal or less than the value of the current row. Let us look at the execution plan:

What happens is that the database fetches all the rows from the table and using a nested loop, it again fetches the rows from which the sum is calculated. This can also be seen in the statistics:

  1. Table 'RunTotalTestData'. Scan count 15, logical reads 30, physical reads 0...

Using join

Another variation is to use join. Now the query could look like:

  1. --------------------------------------------------------------------
  2. -- using join
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, SUM(b.Value)
  5. FROM RunTotalTestData a,
  6. RunTotalTestData b
  7. WHERE b.id <= a.id
  8. GROUP BY a.id, a.value
  9. ORDER BY a.id;

The results are the same but the technique is a bit different. Instead of fetching the sum for each row, the sum is created by using a GROUP BY clause. The rows are cross joined restricting the join only to equal or smaller ID values in B. The plan:

The plan looks somewhat different and what actually happens is that the table is read only twice. This can be seen more clearly with the statistics.

  1. Table 'RunTotalTestData'. Scan count 2, logical reads 31...

The correlated scalar query has a calculated cost of 0.0087873 while the cost for the join version is 0.0087618. The difference isn't much but then again it has to be remembered that we're playing with extremely small amounts of data.

Using conditions

In real-life scenarios, restricting conditions are often used, so how are conditions applied to these queries. The basic rule is that the condition must be defined twice in both of these variations. Once for the rows to fetch and the second time for the rows from which the sum is calculated.

If we want to calculate the running total for odd value numbers, the correlated scalar version could look like the following:

  1. --------------------------------------------------------------------
  2. -- correlated scalar, subset
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, (SELECT SUM(b.value)
  5. FROM RunTotalTestData b
  6. WHERE b.id <= a.id
  7. AND b.value % 2 = 1)
  8. FROM RunTotalTestData a
  9. WHERE a.value % 2 = 1
  10. ORDER BY a.id;

The results are:

  1. id value runningtotal
  2. -- ----- ------------
  3. 1 1 1
  4. 4 7 8
  5. 5 9 17
  6. 7 13 30
  7. 11 57 87
  8. 13 59 146

And with the join version, it could be like:

  1. --------------------------------------------------------------------
  2. -- with join, subset
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, SUM(b.Value)
  5. FROM RunTotalTestData a,
  6. RunTotalTestData b
  7. WHERE b.id <= a.id
  8. AND a.value % 2 = 1
  9. AND b.value % 2 = 1
  10. GROUP BY a.id, a.value
  11. ORDER BY a.id;

When actually having more conditions, it can be quite painful to maintain the conditions correctly. Especially if they are built dynamically.

Calculating running totals for partitions of data

If the running total needs to be calculated to different partitions of data, one way to do it is just to use more conditions in the joins. For example, if the running totals would be calculated for both odd and even numbers, the correlated scalar query could look like:

  1. --------------------------------------------------------------------
  2. -- correlated scalar, partitioning
  3. --------------------------------------------------------------------
  4. SELECT a.value%2, a.id, a.value, (SELECT SUM(b.value)
  5. FROM RunTotalTestData b
  6. WHERE b.id <= a.id
  7. AND b.value%2 = a.value%2)
  8. FROM RunTotalTestData a
  9. ORDER BY a.value%2, a.id;

The results:

  1. even id value running total
  2. ---- -- ----- -------------
  3. 0 2 2 2
  4. 0 3 4 6
  5. 0 6 12 18
  6. 0 8 16 34
  7. 0 9 22 56
  8. 0 10 42 98
  9. 0 12 58 156
  10. 0 14 60 216
  11. 1 1 1 1
  12. 1 4 7 8
  13. 1 5 9 17
  14. 1 7 13 30
  15. 1 11 57 87
  16. 1 13 59 146

So now the partitioning condition is added to the WHERE clause of the scalar query. When using the join version, it could be similar to:

 
  1. --------------------------------------------------------------------
  2. -- with join, partitioning
  3. --------------------------------------------------------------------
  4. SELECT a.value%2, a.id, a.value, SUM(b.Value)
  5. FROM RunTotalTestData a,
  6. RunTotalTestData b
  7. WHERE b.id <= a.id
  8. AND b.value%2 = a.value%2
  9. GROUP BY a.value%2, a.id, a.value
  10. ORDER BY a.value%2, a.id;

With SQL Server 2012

SQL Server 2012 makes life much more simpler. With this version, it's possible to define an ORDER BY clause in the OVER clause.

So to get the running total for all rows, the query would look:

  1. --------------------------------------------------------------------
  2. -- Using OVER clause
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, SUM(a.value) OVER (ORDER BY a.id)
  5. FROM RunTotalTestData a
  6. ORDER BY a.id;

The syntax allows to define the ordering of the partition (which in this example includes all rows) and the summary is calculated in that order.

To define a condition for the data, it doesn't have to be repeated anymore. The running total for odd numbers would look like:

  1. --------------------------------------------------------------------
  2. -- Using OVER clause, subset
  3. --------------------------------------------------------------------
  4. SELECT a.id, a.value, SUM(a.value) OVER (ORDER BY a.id)
  5. FROM RunTotalTestData a
  6. WHERE a.value % 2 = 1
  7. ORDER BY a.id;

And finally, partitioning would be:

  1. --------------------------------------------------------------------
  2. -- Using OVER clause, partition
  3. --------------------------------------------------------------------
  4. SELECT a.value%2, a.id, a.value, SUM(a.value) OVER (PARTITION BY a.value%2 ORDER BY a.id)
  5. FROM RunTotalTestData a
  6. ORDER BY a.value%2, a.id;

What about the plan? It's looking very different. For example, the simple running total for all rows looks like:

And the statistics:

  1. Table 'Worktable'. Scan count 15, logical reads 85, physical reads 0...
  2. Table 'RunTotalTestData'. Scan count 1, logical reads 2, physical reads 0...

Even though the scan count looks quite high at first glance, it isn't targeting the actual table but a worktable. The worktable is used to store intermediate results which are then read in order to create the calculated results.

The calculated cost for this query is now 0.0033428 while previously with the join version, it was 0.0087618. Quite an improvement.

References

from http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx

I had always believed there were three different methods for calculating a running total using TSQL:
1.     Use a nested sub-query
2.     Use a self join
3.     Use Cursors
My own personal preference was to use the cursors option. If the cursor guidelines are followed, I've always found this to be the quickest, because the other two methods involve multiple scans of the table. The key for the cursor method is to ensure the data you are "cursoring" through is in the correct order, as the query optimzier does not understand cursors. This usually means cursoring through the data by clustered index, or copying the data into a temp table / table var first, in the relevant order.
A blog posted by Garth Wells back in 2001 gives these three techniques (http://www.sqlteam.com/article/calculating-running-totals)
I came across a fourth technique for the running total calculation, which is related to the cursor method. Like the cursor method, it involves a single scan of the source table, then inserting the calculated running total for each row into a temp table or table variable. However, instead of using a cursor, it makes use of the following UPDATE command syntax:
UPDATE table
SET variable = column = expression
The TSQL to calculate the running total is:
 
DECLARE @SalesTbl TABLE (DayCount smallint, Sales money, RunningTotal money)
 
DECLARE @RunningTotal money
 
INSERT INTO @SalesTbl 
SET @RunningTotal = 0
 
SELECT DayCount, Sales, null
FROM Sales
ORDER BY DayCount
 
UPDATE @SalesTbl
SET @RunningTotal = RunningTotal = @RunningTotal + Sales
FROM @SalesTbl
 
SELECT * FROM @SalesTbl
 
I tested this query along with the other three methods on a simple set of test data (actually the same test data from Garth Wells’ blog mentioned above).
The results of my test runs are:
Method Time Taken
Nested sub-query 9300 ms
Self join 6100 ms
Cursor 400 ms
Update to local variable 140 ms
 
I was surprised just how much faster using the “Update to a local variable” method was. I expected it to be similar to the cursor method, as both involve a single scan of the source table, and both calculate the running total once only for each row in the table. The Nested Sub-query and Self join methods are so much slower because they involve the repeated recalculation of all of the previous running totals.

Note: There is a pretty big assumption in using the “Update to local variable” method. This is that the Update statement will update the rows in the temp table in the correct order. There is no simple way to specify the order for an Update statement, so potentially this method could fail, although I have not seen this actually happen yet!

I think that if I use a table variable, then the update will probably be in the correct order, because there are no indexes for the query optimizer to use, and parallellism will not occur. However, I can't be sure about this!

The following script was used to create the test data:
CREATE TABLE Sales (DayCount smallint, Sales money)
CREATE CLUSTERED INDEX ndx_DayCount ON Sales(DayCount)
go
INSERT Sales VALUES (1,120)
INSERT Sales VALUES (2,60)
INSERT Sales VALUES (3,125)
INSERT Sales VALUES (4,40)
 
DECLARE @DayCount smallint, @Sales money
SET @DayCount = 5
SET @Sales = 10
 
WHILE @DayCount < 5000
BEGIN
INSERT Sales VALUES (@DayCount,@Sales)
SET @DayCount = @DayCount + 1
SET @Sales = @Sales + 15
END
 
The queries used in my tests for the other three methods are posted below:
1.     Nested Sub-query. ALSO KNOW AS correlated scalar query
SELECT DayCount,
       Sales,
       Sales+COALESCE((SELECT SUM(Sales)
                      FROM Sales b
                      WHERE b.DayCount < a.DayCount),0)
                         AS RunningTotal
FROM Sales a
ORDER BY DayCount
2.    Self join
SELECT a.DayCount,
       a.Sales,
       SUM(b.Sales)
FROM Sales a
INNER JOIN Sales b
ON (b.DayCount <= a.DayCount)
GROUP BY a.DayCount,a.Sales
ORDER BY a.DayCount,a.Sales
3.     Cursor
 
DECLARE @SalesTbl TABLE (DayCount smallint, Sales money, RunningTotal money)
 
DECLARE @DayCount smallint,
        @Sales money,
        @RunningTotal money
 
SET @RunningTotal = 0
 
DECLARE rt_cursor CURSOR
FOR
SELECT DayCount, Sales
FROM Sales
ORDER BY DayCount
 
OPEN rt_cursor
 
FETCH NEXT FROM rt_cursor INTO @DayCount,@Sales
 
WHILE @@FETCH_STATUS = 0
 BEGIN
 SET @RunningTotal = @RunningTotal + @Sales
 INSERT @SalesTbl VALUES (@DayCount,@Sales,@RunningTotal)
 FETCH NEXT FROM rt_cursor INTO @DayCount,@Sales
 END
 
CLOSE rt_cursor
DEALLOCATE rt_cursor

SELECT * FROM @SalesTb

参考 http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sqlserver

CTE:

with CTE_RunningTotal
as
(
select T.ord, T.total, T.total as running_total
from #t as T
where T.ord = 0
union all
select T.ord, T.total, T.total + C.running_total as running_total
from CTE_RunningTotal as C
inner join #t as T on T.ord = C.ord + 1
)

select C.ord, C.total, C.running_total
from CTE_RunningTotal as C
option (maxrecursion 0)

SQL Server 2012 Sum() Over() 

select id,somedate,somevalue, sum(somevalue) over(order by somedate rows unbounded preceding) as runningtotal
from TestTable

Cross Apply: very simmilar to the correlated scalar query

select t.id,t.somedate,t.somevalue,rt.runningTotal

from TestTable t cross apply (select sum(somevalue) as runningTotal from TestTable where somedate <= t.somedate ) as rt

order by t.somedate

Calculating simple running totals in SQL Server的更多相关文章

  1. A simple way to monitor SQL server SQL performance.

    This is all begins from a mail. ... Dear sir: This is liulei. Thanks for your help about last PM for ...

  2. <转>SQL Server CROSS APPLY and OUTER APPLY

    Problem SQL Server 2005 introduced the APPLY operator, which is like a join clause and it allows joi ...

  3. Migrating Oracle on UNIX to SQL Server on Windows

    Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...

  4. SQL Server数据库的三种恢复模式:简单恢复模式、完整恢复模式和大容量日志恢复模式(转载)

    SQL Server数据库有三种恢复模式:简单恢复模式.完整恢复模式和大容量日志恢复模式: 1.Simple 简单恢复模式, Simple模式的旧称叫”Checkpoint with truncate ...

  5. How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络

    SQL Server database administrators may frequently need in especially development and test environmen ...

  6. SQL Server 2008性能故障排查(一)——概论

    原文:SQL Server 2008性能故障排查(一)--概论 备注:本人花了大量下班时间翻译,绝无抄袭,允许转载,但请注明出处.由于篇幅长,无法一篇博文全部说完,同时也没那么快全部翻译完,所以按章节 ...

  7. SQL Server数据库有三种恢复模式:简单恢复模式、完整恢复模式和大容量日志恢复模式

    SQL Server数据库有三种恢复模式:简单恢复模式.完整恢复模式和大容量日志恢复模式: 1.Simple 简单恢复模式, Simple模式的旧称叫”Checkpoint with truncate ...

  8. SQL Server 查询请求

    当SQL Server 引擎接收到用户发出的查询请求时,SQL Server执行优化器将查询请求(Request)和Task绑定,并为Task分配一个Workder,SQL Server申请操作系统的 ...

  9. 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复

    源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...

随机推荐

  1. sizeToFit()使用心得

    sizeToFit()使用心得: 很多的初学者,包括我在内,当初在学习的时候,特别纠结什么时候用这个sizeToFit(). 下面我就来分享一下我的一些使用心得. 一.我们先来看看官方文档对sizeT ...

  2. swift -- 学习记录

    先把疯狂的swift这本书大致看了一遍 2016.7.13 因为实在是太闲,所以决定把公司的应用用swift写一遍 然后顺便看看swift的官方文档 这里有一个官文的中文翻译,感动啊 http://w ...

  3. php 配置正确的时间

    关于php时区时间错误问题 date 当前时间 时差 当地 本地date_default_timezone_set 之前有一个遗留问题,就是echo date("y-m-d h:i:s&qu ...

  4. Html - Iframe

    父页面调用子页面 //用这个对象调用子页面的函数或者dom var myiframe = $("#right_iframe")[0].contentWindow; 子页面调用父页面 ...

  5. 【液晶模块系列基础视频】4.1.X-GUI图形界面库-画线画圆等函数简介

    [液晶模块系列基础视频]4.1.X-GUI图形界面库-画线画圆等函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地 ...

  6. 20. 求阶乘序列前N项和

    求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...

  7. 使用AndroidStudio自动生成JavaDoc文档

    选中所需要生成的Module,找到工具栏上的 Tools,选择 Generate JavaDoc 出现如图的窗口 选择输出路径,就自动生成. 如果出现编码错误提示,则在 Other command l ...

  8. 下载、运行docker

    Get the Linux binary To download the latest version for Linux, use the following URLs: https://get.d ...

  9. 让Win8自动登录免输入密码的小技巧

    按Win+R键,输出“netplwiz”,单击“确定”,弹出“用户帐户”窗口.将第一个画框上的勾选去掉——应用——弹出自动登录输入你的密码——确定——确定完成.重启看看电脑是不是不用输入密码也可以自动 ...

  10. thinkphp的save方法失败

    如果用下面的方式更新数据时, $data['link_phone'] = I('post.link_phone'); $flag1 = $order->save ($data); $data一定 ...