大数据pyspark sql分析函数lead()和lag()

大数据pyspark sql分析函数lead()和lag(),第1张

数据pyspark sql分析函数lead()和lag()

1 功能

Lag和Lead函数可以在一次查询中取出同一字段的前N行的数据和后N行的值

2 语法

lag(col, offset=1, default=None)

col 被对比的字段

offset 偏移量

default 默认值

3 不多说,直接上案例

session_window = Window.partitionBy("user_id", "sponsor_id").orderBy(functions.col("event_time").asc())
    diff_df = df.withColumn("lead", functions.lead("event_time", 1).over(session_window))
        .withColumn("lag", functions.lag("event_time", 1).over(session_window))
        .filter("user_id is not null and user_id !='' and sponsor_id is not null")
        .select("page", "sponsor_id", "user_id", "event_time", "lead","lag")

运行效果

4 结论

很明显,lead是往后偏移量,lag是往前一行偏移

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5701888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存