Click here to Skip to main content
Rate this: bad
good
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 1:36am
Edited 17-Nov-12 1:38am
Nelek51.4K
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 at 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
0 Sergey Alexandrovich Kryukov 353
1 OriginalGriff 304
2 George Jonsson 258
3 Shemeemsha RA 138
4 Animesh Datta 130
0 OriginalGriff 5,985
1 Sergey Alexandrovich Kryukov 5,341
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
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