Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Mysql commutative sum of each row

Tags:

database

mysql

I want to achieve by mysql query

+------+---------+
| frq  | -any-   |
+------+---------+
|   10 |      10 |
|   15 |10+15=25 |
|   15 |25+15=40 |
+------+---------+

please help with code references, thanks

like image 796
Ish Avatar asked May 11 '09 09:05

Ish


2 Answers

IMHO, that should be handled by program logic, and not SQL. If you still want to...:

   SELECT a.frq, sum(b.frq)
     FROM table a
     JOIN table b ON a.id >= b.id
 GROUP BY a.frq
like image 181
Tordek Avatar answered Oct 23 '22 00:10

Tordek


MySQL Forum helped me !!!

mysql> set @my_var=0;
mysql> select frq, @my_var:=@my_var+frq as commutative_sum from `mytable`

This works well with my mysql routines.

like image 38
Ish Avatar answered Oct 23 '22 02:10

Ish