LAG
LAG(col,n,DEFA
ULT) 用于统计窗口内往上第n行值
参数1为列名,参数2为往上第n行(可选,默认为1),参数3为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)
LEAD
与LAG相反
LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值
参数1为列名,参数2为往下第n行(可选,默认为1),参数3为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL
应用场景:取用户的上次登录时间,下次登陆时间
select name, log_time ,lead(log_time,2) over(partition by user_name order by log_time asc ) as rtime from leg_lead;
select name, time ,lag(time,1) over(partition by name order by time asc ) as rtime from leg_lead;