Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 recursion using aggregate function

Author  Topic 

Minchu
Starting Member

1 Post

Posted - 2012-10-30 : 16:32:31
I have a table having only one column
TabValue
1
2
3
4

I need the output to be
TabValue
1
3
6
10

First row is as it is, second row is 1+2(i.e first row+second row),
third row is 1+2+3 (i.e first row+second row+third row) and so on.
Can i use CTEs with aggregated function. It is throwing error

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 16:55:28
[code]
SELECT CummTabValue
FROM Table t
CROSS APPLY (SELECT SUM(TabValue) AS CummTabValue
FROM Table
WHERE TabValue < = t.TabValue
)t1
ORDER BY TabValue
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-11-02 : 05:11:47
Also see Running total method
http://beyondrelational.com/modules/2/blogs/70/posts/10865/quirky-update-in-sql-server.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -