Is there a way to create code for cumulative variance using sql and not excel spreadsheets?

Is there a way to create sql code for cumulative variance?

col1 | cum var
21                  21      =21+0
-34                  -13    =21+(-34)
45                   32     =  -13+45
56                   88     = 32 + 56
-78                  10     = 88 + (-78)
Tay JohnsonAsked:
Who is Participating?
 
PortletPaulConnect With a Mentor freelancerCommented:
SUM(...) OVER(ORDER BY ...)
select
col1, sum(col1) over(order by rownum) cum_var
from table1
order by rownum;

| COL1 | CUM_VAR |
|------|---------|
|   21 |      21 |
|  -34 |     -13 |
|   45 |      32 |
|   56 |      88 |
|  -78 |      10 |

Open in new window

see: http://sqlfiddle.com/#!4/4516b/1
0
 
Nitin SontakkeDeveloperCommented:
Yes, I believe, however, you should also have a column (or set of columns) to order these rows.
0
 
Tay JohnsonAuthor Commented:
Thanks for the solution and a link to create my own sample code. Awesome
0
 
PortletPaulfreelancerCommented:
Thanks for the quick closure, cheers. Paul.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.