窗口函数

1.相关函数说明

COVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变而变化

CURRENT ROW:当前行

n PRECEDING:往前n行数据

n FOLLOWING:往后n行数据

UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDED FOLLOWING表示到后面的终点

LAG(col,n):往前第n行数据

LEAD(col,n):往后第n行数据

NTILE(n):把有序分区中的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,NTILE返回此行所属的组的编号。注意:n必须为int类型。

2.数据准备:name,orderdate,cost

jack,--,

tony,--,

jack,--,

tony,--,

jack,--,

jack,--,

tony,--,

jack,--,

mart,--,

mart,--,

neil,--,

mart,--,

neil,--,

mart,--,

3.需求

(1)查询在2017年4月份购买过的顾客及总人数

(2)查询顾客的购买明细及月购买总额

(3)上述的场景,要将cost按照日期进行累加

(4)查询顾客上次的购买时间

(5)查询前20%时间的订单信息

4.创建本地business.txt,导入数据

[atguigu@hadoop102 datas]$ vi business.txt

5.创建hive表并导入数据

create table business(

name string,

orderdate string,

cost int

) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

load data local inpath "/opt/module/datas/business.txt" into table business;

6.按需求查询数据

(1)查询在2017年4月份购买过的顾客及总人数

select name,count(*) over ()

from business

where substring(orderdate,1,7) = '2017-04'

group by name;

(2)查询顾客的购买明细及月购买总额

select name,orderdate,cost,sum(cost) over(partition by month(orderdate)) from

business;

(3)上述的场景,要将cost按照日期进行累加

select name,orderdate,cost,

sum(cost) over() as sample1,--所有行相加

sum(cost) over(partition by name) as sample2,--按name分组,组内数据相加

sum(cost) over(partition by name order by orderdate) as sample3,--按name分组,组内数据累加

sum(cost) over(partition by name order by orderdate rows between UNBOUNDED PRECEDING and current row ) as sample4 ,--和sample3一样,由起点到当前行的聚合

sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING and current row) as sample5, --当前行和前面一行做聚合

sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING AND 1 FOLLOWING ) as sample6,--当前行和前边一行及后面一行

sum(cost) over(partition by name order by orderdate rows between current row and UNBOUNDED FOLLOWING ) as sample7 --当前行及后面所有行

from business;

(4)查看顾客上次的购买时间

select name,orderdate,cost,

lag(orderdate,1,'1900-01-01') over(partition by name order by orderdate ) as time1, lag(orderdate,2) over (partition by name order by orderdate) as time2

from business;

(5)查询前20%时间的订单信息

select * from (

select name,orderdate,cost, ntile(5) over(order by orderdate) sorted

from business

) t

where sorted = 1;

最新文章

  1. java 将字符串下载为文本文件
  2. ccConfig(设置一些底层接口状态:是否支持动作叠加 设置fps更新间隔和位置 是否画边框等。。)
  3. 如何使用php session
  4. configure: error: Cannot find php-config. Please use --with-php-config=PATH 错误的解决方案
  5. app打包常用操作
  6. 如何用Visio画venn(维恩)图
  7. python_如何快速安装第三方库?
  8. iot会议纪要 20180105
  9. centos备份多个数据库
  10. Pandas数据处理+Matplotlib绘图案例
  11. 【shiro】(5)---基于Shiro的权限管理
  12. HDU6446 Tree and Permutation(树上DP)
  13. js异步加载的5种方式
  14. 代码面试集锦 2 - Google
  15. 使用gtest对DLL工程进行单元测试的实践
  16. PHP面向对象 实例化 构造函数 封装 继承 静态
  17. css3半圆
  18. nodejs初印象
  19. +QFTPOPEN: 603,0 怎么把这样一个字符串中的 603 提取出来给一个 uint32_t 的变量那
  20. 【设计模式】template method(模板方法)-- 类行为型模式5.10

热门文章

  1. php+google/baidu翻译接口
  2. Go 缓冲信道和非缓冲信道
  3. 《LOST》 电视
  4. 切换了webview 定位不了的解决方法 (还没有试,记录在此)
  5. php的语句
  6. Zabbix 更改监控项的应用级
  7. VRRP、Track与NQA联动配置举例(Master监视上行链路)
  8. spring 定时任务 scheduled Cron表达式
  9. 阿里直播在线人数只统计rtmp格式的播放源
  10. MySQL 索引建立原则及注意事项