0

I am using Query to calculate cumulative values like :

..sum(Total) over(order by Total desc) As Cumulative

But it calculates the same value at a time.

Here is my output:

Error type  Total   Cumulative
E1          10      10
E2          5       20
E3          5       20
E4          3       26
E5          3       26

I would like the following:

Error type  Total   Cumulative
E1          10      10
E2          5       15
E3          5       20
E4          3       23
E5          3       26

I'm not sure how to figure this out.

Tanner
  • 22,205
  • 9
  • 65
  • 83
Jack
  • 510
  • 3
  • 6
  • 22
  • 1
    Duplicate of http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sqlserver ? – Astrogat Jul 17 '15 at 09:03
  • possible duplicate of [SQL Server Cumulative Sum by Group](http://stackoverflow.com/questions/17971988/sql-server-cumulative-sum-by-group) – Tanner Jul 17 '15 at 09:06
  • Just change the order by to: `(order by [Error type])` to get your desired output. Check this [SQL Fiddle Demo](http://sqlfiddle.com/#!6/1c6d9/1) – Tanner Jul 17 '15 at 09:07

1 Answers1

-1

i think you are looking for this

select Error type , Total, 
sum(total) over (order by total desc Rows between Unbounded Preceding and  CURRENT ROW) as Cumulative 
from Table
Dhaval
  • 2,341
  • 1
  • 13
  • 16
  • If the downvoter could explain why this is bad, it would help everyone. It _looks_ bad to me, but I am not familiar enough with the newer keywords to say why. Care to share? – underscore_d Jul 17 '15 at 09:23
  • @ underscore_d But it helps people most of the time. – Jack Jul 20 '15 at 11:08