sql - how to do lag operation in mysql -


guys want use analytical function lag in mysql. in oracle supported can't in mysql. can me how perform lag operation in mysql? example

uid                        operation  1                         logged-in  2                         view content  3                         review 

i want use lag function output follows

uid                        operation              lagoperation  1                         logged-in                  2                         view content           logged-in  3                         review                 view content 

does mysql support lag function???

you can emulate user variables:

select uid, operation, previous_operation ( select y.* , @prev previous_operation , @prev := operation your_table y , (select @prev:=null) vars order uid ) subquery_alias 

here initialize variable(s). it's same writing set @prev:=null; before writing query.

, (select @prev:=null) vars 

then order of these statements in select clause important:

, @prev previous_operation , @prev := operation 

the first displays variables value, second assigns value of current row variable.

it's important have order by clause, output otherwise not deterministic.

all put subquery out of aesthetic reasons,... filter out this

, @prev := operation 

column.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -