Deepesh
Deepesh

Reputation: 840

Adding a previous value with the current value in mysql

I have two columns namely zone and total_bill_amount, I want to add the previous values with the current value of a column..I have used

 select  zone, total_bill_amount, sum(total_bill_amount) as Total_Bill
 from Cal_Amount  
 where cluster_number = 'clust 2' 
 group by zone,total_bill_amount;

 1. zone       total_bill_amount   Total_Bill                           

 2. ABC          45                    45                               

 3. PQR          78                    123                              

 4. XYZ          16                    139

45 = 45 45+78 = 123 123 + 16 = 139

Upvotes: 0

Views: 554

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72175

You can do it using variables:

select zone, total_bill_amount,
       @total := @total + total_bill_amount AS Total_Bill
from Cal_Amount
where cluster_number = 'clust 2'        
cross join (select @total := 0) as var
order by zone

You have to substiute zone with the column that specifies order in your table, in case zone is not the one.

Upvotes: 1

Related Questions