Click here to Skip to main content
12,629,493 members (35,418 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: MySQL
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

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

please help me

[edit]code block added[/edit]
Posted 17-Nov-12 2:36am
Updated 17-Nov-12 2:38am
Nelek110.8K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100