Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Update a running total

Posted on 2014-04-15
Medium Priority
174 Views
MS SQL Server 2008.

I have a table that has the following columns

visitdate date
Eye int
Treatmentcount int
RunningTotal int

where Eye can be null, 1(right) or 2(left).  Null means no treatment on this date.

Treatmentcount can be null, or a number less than 10

The table is ordered desc on Visitdate.

``````Visit date   Eye           TreatmentCount   RunningTotal
2012-05-01   1              2
2012-04-01
2012-03-01   1              4
2012-02-01   1              1
``````
How can i update the RunningTotal column so it shows the total number of treatments at each visit?
``````Visit date   Eye           TreatmentCount   RunningTotal
2012-05-01   1              2                      7
2012-04-01
2012-03-01   1              4                      5
2012-02-01   1              1                      1
``````
0
Question by:soozh
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 39

Expert Comment

ID: 40000983
on which basis you wanto calcutae running total ?
0

Author Comment

ID: 40001010
not sure what you mean, but you can see the running total in the second table.
0

LVL 13

Accepted Solution

sameer2010 earned 2000 total points
ID: 40001213
Try this if you meant adding totals based on dates
``````drop table #t
create table #t(visitdate date,
Eye int,
Treatmentcount int,
RunningTotal int)
insert into #t values('2012-05-01',1,2,null)
insert into #t values('2012-04-01',null,null,null)
insert into #t values('2012-03-01',1,4,null)
insert into #t values('2012-02-01',1,1,null)

select visitdate,Eye,Treatmentcount,(select sum(treatmentcount) from #t b where b.Eye=a.Eye and b.visitdate <=a.visitdate) as runningtotal
from #t a

update #t set RunningTotal = (select sum(treatmentcount) from #t b where b.Eye=a.Eye and b.visitdate <=a.visitdate)
from #t a
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
###### Suggested Courses
Course of the Month9 days, left to enroll