Solved

Update a running total

Posted on 2014-04-15
3
171 Views
Last Modified: 2014-04-15
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

Open in new window

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

Open in new window

0
Comment
Question by:soozh
3 Comments
 
LVL 39

Expert Comment

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

Author Comment

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

Accepted Solution

by:
sameer2010 earned 500 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

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL BCP Extra Lines Between Records 2 24
SSAS Hierarchy with columns with folder names 10 19
AJAX pass along a variable 3 46
PolyServe for SQL server 13 32
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question