Not able to Calculate Running Total in sql server

my table looks like as bellow


ID             Person          bal     N0          running  Sum
1                  1                 5         1                5
2                   2                5          2              10
3                   2                5          3              10
4                   3                10        4               20
5                   4                6          5                26
6                   4                6          6                26
7                    5                1         7                27



here i want Running total based on distinct value   Person column   and bal column  value  sum


if person column  value is reapting at that at that time bal value should be added to running total
codeoxygenAsked:
Who is Participating?
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.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
check out this script:
declare @t table (id int , person int, bal int, no int )
insert into @t values(1,                  1                , 5        , 1      )
insert into @t values(2,                   2               , 5        ,  2     )
insert into @t values(3,                   2               , 5        ,  3      )
insert into @t values(4,                   3               , 10       , 4        )
insert into @t values(5,                   4               , 6        ,  5        )
insert into @t values(6,                   4               , 6        ,  6         )
insert into @t values(7,                    5              ,  1       ,  7          )

select t.* 
, (select sum(b.bal)
		from @t b
	  where b.id <= t.id  
	  and not exists(select null from @t o
	        where o.person = b.person  
			  and o.bal = b.bal
			  and o.id < b.id 
			  )
		) running
from @t t

Open in new window


output:
id          person      bal         no          running
----------- ----------- ----------- ----------- -----------
1           1           5           1           5
2           2           5           2           10
3           2           5           3           10
4           3           10          4           20
5           4           6           5           26
6           4           6           6           26
7           5           1           7           27

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codeoxygenAuthor Commented:
If i have to calculate similarly for 3 more coulumns based on the person column ,


should i repeat the  subqurey  3 times or any other bettter way
0
PortletPaulfreelancerCommented:
are you using MS SQL 2012 or later?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

codeoxygenAuthor Commented:
2008
0
codeoxygenAuthor Commented:
ID             Person          bal     N0          running  Sum     Fine             fine running sum

1                  1                 5         1                5                          2                      2
2                   2                5          2              10                         3                      5
3                   2                5          3              10                         3                      5
4                   3                10        4               20                        1                      6
5                   4                6          5                26                       2                      8
6                   4                6          6                26                       2                      8
7                    5                1         7                27                       5                      13
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>should i repeat the  subqurey  3 times or any other bettter way
you will have to repear the subquery 3 times when using this technique.
with a CTE expression, we could do intermediate steps (to perform the "distinct" in a CTE expression first), but still we will need a subquery for each expression./
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.