概念

测试数据

-- 数据准备:
姓名,购买日期,购买数量
saml    2018-01-01  10
saml    2018-01-08  55
tony    2018-01-07  50
saml    2018-01-05  46
tony    2018-01-04  29
tony    2018-01-02  15
saml    2018-02-03  23
mart    2018-04-13  94
saml    2018-04-06  42
mart    2018-04-11  75
mart    2018-04-09  68
mart    2018-04-08  62
neil    2018-05-10  12
neil    2018-06-12  80-- 创建order表:
create table if not exists t_order
(name      string,orderdate string,cost      int
)  row format delimited fields terminated by '\t';
--加载数据:
load data local inpath "./data/order.txt" into table t_order;

lag函数

lag函数.三个参数,第一个列名,第二个往前多少行,第三个默认值

– 列出每个人详细信息和上一次购买的数量

select *,lag(cost,1,0)  over (distribute by name sort by orderdate) `pre cost` from t_order;
+----+----------+----+--------+
|name|orderdate |cost|pre cost|
+----+----------+----+--------+
|    |NULL      |NULL|0       |
|mart|2018-04-08|62  |0       |
|mart|2018-04-09|68  |62      |
|mart|2018-04-11|75  |68      |
|mart|2018-04-13|94  |75      |
|neil|2018-05-10|12  |0       |
|neil|2018-06-12|80  |12      |
|saml|2018-01-01|10  |0       |
|saml|2018-01-05|46  |10      |
|saml|2018-01-08|55  |46      |
|saml|2018-02-03|23  |55      |
|saml|2018-04-06|42  |23      |
|tony|2018-01-02|15  |0       |
|tony|2018-01-04|29  |15      |
|tony|2018-01-07|50  |29      |
+----+----------+----+--------+

– 列出每个人详细信息和上上次购买的数量

select *,lag(cost,2,0)  over (distribute by name sort by orderdate) `pre cost` from t_order;

lead函数

列出每个人详细信息和大上次购买的数量,以及当前日期的下一次购买的数量

select *,lag(cost,2,0)  over (distribute by name sort by orderdate) `pre cost`,
lead(cost,1,0) over (distribute by name sort by orderdate) `next cost`
from t_order;
+----+----------+----+--------+---------+
|name|orderdate |cost|pre cost|next cost|
+----+----------+----+--------+---------+
|    |NULL      |NULL|0       |0        |
|mart|2018-04-08|62  |0       |68       |
|mart|2018-04-09|68  |0       |75       |
|mart|2018-04-11|75  |62      |94       |
|mart|2018-04-13|94  |68      |0        |
|neil|2018-05-10|12  |0       |80       |
|neil|2018-06-12|80  |0       |0        |
|saml|2018-01-01|10  |0       |46       |
|saml|2018-01-05|46  |0       |55       |
|saml|2018-01-08|55  |10      |23       |
|saml|2018-02-03|23  |46      |42       |
|saml|2018-04-06|42  |55      |0        |
|tony|2018-01-02|15  |0       |29       |
|tony|2018-01-04|29  |0       |50       |
|tony|2018-01-07|50  |15      |0        |
+----+----------+----+--------+---------+

–列出每个人的详细信息和上一次购买数据,以及与上一次购买数据的差值

select * ,lag(cost,1,0) over(distribute by name sort by orderdate) `pre_cost`,cost-lag(cost,1,0) over(distribute by name sort by orderdate) `diff`
from t_order;
+----+----------+----+--------+----+
|name|orderdate |cost|pre_cost|diff|
+----+----------+----+--------+----+
|    |NULL      |NULL|0       |NULL|
|mart|2018-04-08|62  |0       |62  |
|mart|2018-04-09|68  |62      |6   |
|mart|2018-04-11|75  |68      |7   |
|mart|2018-04-13|94  |75      |19  |
|neil|2018-05-10|12  |0       |12  |
|neil|2018-06-12|80  |12      |68  |
|saml|2018-01-01|10  |0       |10  |
|saml|2018-01-05|46  |10      |36  |
|saml|2018-01-08|55  |46      |9   |
|saml|2018-02-03|23  |55      |-32 |
|saml|2018-04-06|42  |23      |19  |
|tony|2018-01-02|15  |0       |15  |
|tony|2018-01-04|29  |15      |14  |
|tony|2018-01-07|50  |29      |21  |
+----+----------+----+--------+----+

first_value函数与last_value函数

求分组排序后的第一个值

select *,first_value(cost) over(distribute by name sort by orderdate)
from t_order;
+----+----------+----+----+
|name|orderdate |cost|c1  |
+----+----------+----+----+
|    |NULL      |NULL|NULL|
|mart|2018-04-08|62  |62  |
|mart|2018-04-09|68  |62  |
|mart|2018-04-11|75  |62  |
|mart|2018-04-13|94  |62  |
|neil|2018-05-10|12  |12  |
|neil|2018-06-12|80  |12  |
|saml|2018-01-01|10  |10  |
|saml|2018-01-05|46  |10  |
|saml|2018-01-08|55  |10  |
|saml|2018-02-03|23  |10  |
|saml|2018-04-06|42  |10  |
|tony|2018-01-02|15  |15  |
|tony|2018-01-04|29  |15  |
|tony|2018-01-07|50  |15  |
+----+----------+----+----+

求分组排序后的最后一个值,但是相当当前行来说,当前行就是最后一行
如下结果并不是我们想要的,换种思路

select *,last_value(cost) over(distribute by name sort by orderdate)
from t_order;
+----+----------+----+----+
|name|orderdate |cost|c1  |
+----+----------+----+----+
|    |NULL      |NULL|NULL|
|mart|2018-04-08|62  |62  |
|mart|2018-04-09|68  |68  |
|mart|2018-04-11|75  |75  |
|mart|2018-04-13|94  |94  |
|neil|2018-05-10|12  |12  |
|neil|2018-06-12|80  |80  |
|saml|2018-01-01|10  |10  |
|saml|2018-01-05|46  |46  |
|saml|2018-01-08|55  |55  |
|saml|2018-02-03|23  |23  |
|saml|2018-04-06|42  |42  |
|tony|2018-01-02|15  |15  |
|tony|2018-01-04|29  |29  |
|tony|2018-01-07|50  |50  |
+----+----------+----+----+

换种思路求每组的最后一个值

select *,first_value(cost) over(distribute by name sort by orderdate desc)
from t_order;
+----+----------+----+----+
|name|orderdate |cost|c1  |
+----+----------+----+----+
|    |NULL      |NULL|NULL|
|mart|2018-04-13|94  |94  |
|mart|2018-04-11|75  |94  |
|mart|2018-04-09|68  |94  |
|mart|2018-04-08|62  |94  |
|neil|2018-06-12|80  |80  |
|neil|2018-05-10|12  |80  |
|saml|2018-04-06|42  |42  |
|saml|2018-02-03|23  |42  |
|saml|2018-01-08|55  |42  |
|saml|2018-01-05|46  |42  |
|saml|2018-01-01|10  |42  |
|tony|2018-01-07|50  |50  |
|tony|2018-01-04|29  |50  |
|tony|2018-01-02|15  |50  |
+----+----------+----+----+

注意事项

以上函数离开over是不能运行的

总结

  • lead与lag,都是三个参数,列名,向前或向后多少行,默认值
  • first_value可以求分组后的第一个,也可以求分组后的最后一个

hive 开窗函数之lag,lead,first_value,last_value相关推荐

  1. hive的 LAG,LEAD,FIRST_VALUE,LAST_VALUE函数

    1.数据准备 CREATE EXTERNAL TABLE lxw1234 ( cookieid string, createtime string, --页面访问时间 url STRING --被访问 ...

  2. Hive 中的wordCount、Hive 开窗函数

    Hive 中的wordCount.Hive 开窗函数 目录 Hive 中的wordCount.Hive 开窗函数 Hive 中的wordCount Hive 开窗函数 测试数据 建表语句 1.row_ ...

  3. 大数据之Hive:Hive 开窗函数(二)

    目录 前言 1.last_value开窗函数 2.lag开窗函数 3.lead开窗函数 4.cume_dist开窗函数 前言 书接上回,上回重点讲了聚合函数之count开窗函数,first_value ...

  4. hive 开窗函数_Hive的架构剖析

    本文主要介绍Hive的架构和以及HQL的查询阶段,主要内容包括: Hive的架构 架构中的相关组件介绍 HQL的查询阶段 Hive的架构 hive的基本架构图如下图所示: 相关组件介绍 数据存储 Hi ...

  5. hive 开窗函数OVER(PARTITION)详解(一)

    什么是窗口函数? 窗口函数(Window Function) 是 SQL2003 标准中定义的一项新特性,并在 SQL2011.SQL2016 中又加以完善,添加了若干处拓展.窗口函数不同于我们熟悉的 ...

  6. 关于hive开窗函数的问题

    如果over() 则开窗的聚合函数统计的所有行 如果over(partition by xx order by xx) 时,开窗的聚合函数统计的是相同partition by 后的字段按照order ...

  7. Hive开窗函数——排名函数开窗

    排名函数开窗 rank() over 作用:查出指定条件后的进行排名,条件相同排名相同,排名间断不连续. 说明:例如学生排名,使用这个函数,成绩相同的两名是并列,下一位同学空出所占的名次.即:1 1 ...

  8. hive中的开窗函数

    目录 count开窗函数 sum开窗函数 min开窗函数 max开窗函数 avg开窗函数 first_value开窗函数 last_value开窗函数 lag开窗函数.lead开窗函数 cume_di ...

  9. Hive SQL开窗函数详解

    Hive 开窗函数 group by 是分组函数,一组出来一个数据 over() 开窗,针对每一条数据,都有一个独立的组 mk 3 jk 3 mk 3 select orderdate,cost,su ...

最新文章

  1. springboot部署war包为什么tomcat会启动两次
  2. 网络工程师_记录的一些真题_2005上半年上午
  3. rewrite 帮助实现页面静态化
  4. python中append函数合并列表且列表内数字从高到低_35个高级Python知识点总结
  5. android6.0源码分析之Activity启动过程
  6. 月均数据_利用Python进行数据分析(附详细案例)
  7. 使用JDK 8将收藏转换为地图
  8. 《Linux杂记:一》
  9. 初识人工智能(一):数据分析(二):numpy科学计算基础库(一)
  10. Spark MLlib中的协同过滤
  11. v-html解析的相对地址img 显示不出来_还不懂java类加载机制的,建议看下这份阿里技术官总结的笔记!...
  12. 安卓设置keychain_Android 7.0 SEAndroid app权限配置方法
  13. eval() python_如何使用 Python 编写 vim 插件
  14. LeCun:深度学习在信号理解中的强大和局限(视频+PPT)
  15. 红莲之弓矢(进击的巨人主题曲)
  16. java 枚举 扑克牌_Java入门第三季7-1简易扑克牌作业
  17. Android 讯飞语音合成、语音播报(详细步骤+源码)
  18. web全栈工程师技能介绍
  19. ubuntu中U盘硬盘格式化(NTFS,FAT12,FAT16,FAT32,EXT4,EXT3,EXT2)
  20. 学习笔记整理:Photoshop软件应用-图层混合与样式

热门文章

  1. 云服务器常用端口及其含义
  2. 笔记本电脑开机后在桌面上没有计算机图标,电脑开机之后桌面上没有图标怎么处理...
  3. php 有几种打印方法,php 5种打印方式及变量类型,
  4. android 点赞动画_Android MotionLayout动画:续写ConstraintLayout新篇章
  5. 复制一段话,发现收费怎么办,下边帮你解决
  6. 外键查询_详解MySQL数据库删除所有表的外键约束、禁用外键约束相关脚本
  7. 计算机系统的组成一般不包括,建筑设备监控子系统组成一般不包括( )A.中央计算机系统B.布线系统C.DDCD.各类传感器及执 - 作业在线问答...
  8. jQuery Validate focusCleanup: true
  9. python表单防重复提交_传统方式提交表单,防止重复提交问题?
  10. oracle12c asmfd,Oracle 12c新特性--ASMFD(ASM Filter Driver)特性