实现lead,lag窗口函数功能脚本

lead:实现窗口向下移动n行(即下面的行往上移)

lag:实现窗口向上移动n行(即上面的行往下移)

 案例脚本:

with t as
(
    select '10001' as uid,'2021-08-10 10:18:31' as dateline,'url3' as page
    union all
    select '10001' as uid,'2021-08-10 10:01:26' as dateline,'url1' as page
    union all
    select '10001' as uid,'2021-08-10 10:09:48' as dateline,'url2' as page
    union all
    select '10002' as uid,'2021-08-10 09:38:31' as dateline,'url3' as page
    union all
    select '10002' as uid,'2021-08-10 09:21:26' as dateline,'url1' as page
    union all
    select '10002' as uid,'2021-08-10 09:19:48' as dateline,'url2' as page
)
select
  t.*
  ,lead(dateline) over(partition by uid sort by dateline) as dateline_lead
  ,lag(dateline) over(partition by uid sort by dateline) as dateline_lag
from t

业务含义:

        用户在各页面访问的起止时间以及停留时长等。

例1:sql实现类似功能(本例实现向上移动1行)

with t as
(
    select '10001' as uid,'2021-08-10 10:18:31' as dateline,'url3' as page
    union all
    select '10001' as uid,'2021-08-10 10:01:26' as dateline,'url1' as page
    union all
    select '10001' as uid,'2021-08-10 10:09:48' as dateline,'url2' as page
    union all
    select '10002' as uid,'2021-08-10 09:38:31' as dateline,'url3' as page
    union all
    select '10002' as uid,'2021-08-10 09:21:26' as dateline,'url1' as page
    union all
    select '10002' as uid,'2021-08-10 09:19:48' as dateline,'url2' as page
)
,t1 (
    select
     uid,
     dateline,
     row_number() over(distribute by uid sort by dateline asc) as rank,
     page
   from t
)
select
  t1.uid,
  t1.dateline,
  t1.page,
  t2.dateline as dateline_end
from t1 left join t1 as t2  on t1.uid=t2.uid and t1.rank+1=t2.rank
;

数据集t展示如下:

移动后的数据集展示如下:

例2:A表转B表(区别上例)

with t as
(
    select '10001' as uid,'2021-08-10 10:18:31' as dateline,'url3' as page
    union all
    select '10001' as uid,'2021-08-10 10:01:26' as dateline,'url3' as page
    union all
    select '10001' as uid,'2021-08-10 10:09:48' as dateline,'url2' as page
    union all
    select '10001' as uid,'2021-08-10 10:10:03' as dateline,'url2' as page
    union all
    select '10002' as uid,'2021-08-10 09:38:31' as dateline,'url1' as page
    union all
    select '10002' as uid,'2021-08-10 09:21:26' as dateline,'url1' as page
    union all
    select '10002' as uid,'2021-08-10 09:19:48' as dateline,'url1' as page
    union all
    select '10002' as uid,'2021-08-10 09:20:48' as dateline,'url1' as page
)
,t1 (
    select
     uid,
     page,
     dateline,
     row_number() over(partition by uid,page order by dateline asc) as rank
   from t
)
select
  t1.uid,
  t1.page,
  t1.dateline,
  t2.dateline as dateline_end
from t1 left join t1 as t2  on t1.uid=t2.uid and t1.page=t2.page and t2.rank-t1.rank=1
order by 1,2,3
;

A表:

B表:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值