mysql - Can grouped expressions be used with variable assignments? -


i'm trying calculate row differences (like mysql difference between 2 rows of select statement) on grouped result set:

create table test (i int not null auto_increment, int, b int, primary key (i)); insert test (a,b) value (1,1),(1,2),(2,4),(2,8); 

gives

| | b --------- | 1 | 1 | 1 | 2 | 2 | 4 | 2 | 8 

this simple sql group , max(group) result columns:

select      data.a,     max(data.b)     (         select a, b         test         order     ) data group order 

the obvious result is

| | max(data.b) ----------------- | 1 | 2 | 2 | 8 

where i'm failing when want calculate row-by-row differences on grouped column:

set @c:=0; select      data.a,     max(data.b),     @c:=max(data.b)-@c     (         select a, b         test         order     ) data group order 

still gives:

| | max(data.b) | @c:=max(data.b)-@c -------------------------------------- | 1 | 2           | 2 (expected 2-0=2) | 2 | 8           | 8 (expected 8-2=6) 

could highlight why @c variable not updating grouped row grouped row expected?

select data.a      , data.b      , @c := data.b - @c (         select              , max(b) b         test         group      ) data order 

example


Comments

Popular posts from this blog

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

C# random value from dictionary and tuple -

algorithm - Testing tetrahedron-triangle intersection -