Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I have a table called temp_reading in this i have the following columns :

meterid consumption  total

1          100

1           150

1          200

1          300

now i want the total like

meterid    consumption            total

 1             100                 100

 1             150                 250

 1             200                450

 1             300                750


i tried the following query

SQL
select meterid,consumption,sum(consumption) as total from temp_reading group by meterid;


please help me

[edit]code block added[/edit]
Posted
Updated 17-Nov-12 1:38am
v2

1 solution

It looks like you are trying to add a running total to your query.
This article Calculating simple running totals in SQL Server[^] describes several ways this can be achieved.
 
Share this answer
 
Comments
Member 9606980 19-Nov-12 1:22am    
i tried the following query
select a.meterid,a.consumption, (select sum(b.consumption) from temp_reading b where b.meterid<=a.meterid) as total from temp_reading a order by a.meterid;
but i am getting the following result
meterid consumption total
1 450 700
1 100 700
1 50 700
1 100 700
it is caliculating 450+100+50+100 = 700 and this value is displaying
but i want to display like:
meterid consumption total

1 450 450

1 100 550

1 50 600

1 100 700

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