Solved

Update a running total

Posted on 2014-04-15
3
169 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now