lag and lead
The function has the following format:
- The first parameter is the column name,
- The second parameter is the n-th line up (optional, defaults to 1),
- The third parameter is the default value (NULL behavior when the n-th time up, the default value, if not specified, compared with NULL)
lag
LAG (field name, N, the default value) over (Partition by grouping field Order by sort field mode)
lead
Lead (field name, N, the default value) over (Partition by grouping field Order by sort field mode)
Case:
select cookieid, createtime, url, row_number() over (partition by cookieid order by createtime) as rn, LAG(createtime,1,'1970-01-01 00:00:00') over (partition by cookieid order by createtime) as last_1_time, LAG(createtime,2) over (partition by cookieid order by createtime) as last_2_time from cookie.cookie4
select cookieid, createtime, url, row_number() over (partition by cookieid order by createtime) as rn, LEAD(createtime,1,'1970-01-01 00:00:00') over (partition by cookieid order by createtime) as next_1_time, LEAD(createtime,2) over (partition by cookieid order by createtime) as next_2_time from cookie.cookie4;
pandas window function to achieve
pandas shift function is used to implement lag / lead function