# 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)
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeveloperCommented:
Yes, I believe, however, you should also have a column (or set of columns) to order these rows.
0
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 |
``````
see: http://sqlfiddle.com/#!4/4516b/1
0

Experts Exchange Solution brought to you by