List:General Discussion« Previous MessageNext Message »
From:Mauricio Pellegrini Date:August 14 2004 12:29am
Subject:Re: Using SUM in a special way
View as plain text  
Hey!!! thanks thanks thanks to all of you!.
 Your just wonderfull and the help you provide is priceless.

I think I will solve my problem now.

I've read about the use of variables in the manual but
It would have taken me a lot of time to figure something like this.

Thanks again and to all the rest of the people 
who answered my question

Gratefully
Mauricio



On Fri, 2004-08-13 at 11:55, Keith Ivey wrote:
> Mauricio Pellegrini wrote:
> 
> >and would like to obtain this result from a query
> >
> >	Col1	Col2	Col3
> >	1	20	20
> >	1	10	30
> >	1	20	50
> >	2	10	10
> >	2	 5	15
> >	3	10	10
> >
> >Column Col3 should carry forward and sum values from Col2
> >
> Something like this should work, using two variables, @total and @prev:
> 
>    SELECT Col1, Col2, @total := IF(@prev = Col1, @total + Col2, Col2 + 
> (@prev := Col1) - Col1)
>    FROM table_name
>    ORDER BY Col1;
> 
> The way I'm setting @prev every time Col1 changes is a bit klugy (having 
> to add it in and
> then subtract Col1 to fix it), but it seems to work.
> 
> Hmm, if you change the order of the result columns you can avoid the kluge:
> 
>    SELECT Col2, @total := IF(@prev = Col1, @total + Col2, Col2), @prev 
> := Col1
>    FROM table_name
>    ORDER BY Col1;

Thread
Using SUM in a special wayMauricio Pellegrini13 Aug
  • Re: Using SUM in a special waySGreen13 Aug
  • Re: Using SUM in a special wayMark Stafford13 Aug
  • Re: Using SUM in a special wayKeith Ivey13 Aug
    • Re: Using SUM in a special wayMauricio Pellegrini13 Aug