views:

604

answers:

2

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

+2  A: 

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
Tordek
+1 for the Join with a >= .... a.id >= b.id ... interesting : )
codeulike
What if i am have some order by clause with it. Then above logic wont work
Ish Kumar
You should be grouping BY a.id in order to get the intended results back.
duckyflip
@Ish Kumar: Because, as I said, you shouldn't be doing this from SQL. Keeping a variable with a running count will save you a lot of pointless struggle.
Tordek
Thanks @Tordek for your contribution.
Ish Kumar
+1  A: 

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.

Ish Kumar