Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table
like:-
id somedate somevalue
-- -------- ---------
45 01/Jan/09 3
23 08/Jan/09 5
12 02/Feb/09 0
77 14/Feb/09 7
39 20/Feb/09 34
33 02/Mar/09 6

I want To Print Like:-
id somedate somevalue runningtotal
-- -------- --------- ------------
45 01/Jan/09 3 3
23 08/Jan/09 5 8
12 02/Feb/09 0 8
77 14/Feb/09 7 15
39 20/Feb/09 34 49
33 02/Mar/09 6 55


please help me.

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
Posted
Updated 15-Dec-11 17:28pm
v3
Comments
Scubapro 15-Dec-11 9:18am    
What do you mean exactly ? You want an output as above where total is not a field(column) of your Table, or you just want the '1100' ?
Amir Mahfoozi 15-Dec-11 9:25am    
Please explain more.
RaviRanjanKr 15-Dec-11 16:43pm    
please be more specific while asking question. we need more information :)

Another possible solution, something like:
SQL
SELECT a.id, 
       a.narration,
       a.dr, 
       a.cr, 
       SUM(c.calc) AS total
FROM   sometable a,
       (SELECT b.id,
               (b.dr - b.cr) AS calc
        FROM sometable b) c
WHERE c.id <= a.id
GROUP BY a.id,
         a.narration,
         a.dr,
         a.cr;
 
Share this answer
 
This is the solution i made to your problem :

SQL
CREATE TABLE TEMP (id INT identity(1,1), somedate datetime, somevalue int)

insert into temp values ('01/Jan/09',3)
insert into temp values ('08/Jan/09',5)
insert into temp values ('02/Feb/09',0)
insert into temp values ('14/Feb/09',7)
insert into temp values ('20/Feb/09',34)
insert into temp values ('02/Mar/09',6)

select * from temp

create table #temp (id int, somevalue int)

insert into #temp (id, somevalue)
select id, somevalue from temp

select * from #temp

select t.id , convert(varchar,t.somedate, 106), t.somevalue, SUM(tp.somevalue) from temp t, #temp tp
where t.id >= tp.id
group by t.id , t.somedate, t.somevalue

drop table #temp
 
Share this answer
 
Nasty! But possible:
SQL
SELECT ID, narration dr, cr, (SELECT SUM(dr-cr) from myTable where iD < M.iD) as RunTot FROM myTable M
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900