博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(九)Hive分析窗口函数(一) SUM,AVG,MIN,MAX
阅读量:5315 次
发布时间:2019-06-14

本文共 3826 字,大约阅读时间需要 12 分钟。

数据准备

 

 

数据格式

cookie1,2015-04-10,1cookie1,2015-04-11,5cookie1,2015-04-12,7cookie1,2015-04-13,3cookie1,2015-04-14,2cookie1,2015-04-15,4cookie1,2015-04-16,4

 

 

创建数据库及表

create database if not exists cookie;use cookie;drop table if exists cookie1;create table cookie1(cookieid string, createtime string, pv int) row format delimited fields terminated by ',';load data local inpath "/home/hadoop/cookie1.txt" into table cookie1;select * from cookie1;

 

玩一玩SUM

 

查询语句

select    cookieid,    createtime,    pv,    sum(pv) over (partition by cookieid order by createtime rows between unbounded preceding and current row) as pv1,    sum(pv) over (partition by cookieid order by createtime) as pv2,    sum(pv) over (partition by cookieid) as pv3,    sum(pv) over (partition by cookieid order by createtime rows between 3 preceding and current row) as pv4,    sum(pv) over (partition by cookieid order by createtime rows between 3 preceding and 1 following) as pv5,    sum(pv) over (partition by cookieid order by createtime rows between current row and unbounded following) as pv6 from cookie1;

 

 

查询结果

 

 

说明

pv1: 分组内从起点到当前行的pv累积,如,11号的pv1=10号的pv+11号的pv, 12号=10号+11号+12号pv2: 同pv1pv3: 分组内(cookie1)所有的pv累加pv4: 分组内当前行+往前3行,如,11号=10号+11号, 12号=10号+11号+12号, 13号=10号+11号+12号+13号, 14号=11号+12号+13号+14号pv5: 分组内当前行+往前3行+往后1行,如,14号=11号+12号+13号+14号+15号=5+7+3+2+4=21pv6: 分组内当前行+往后所有行,如,13号=13号+14号+15号+16号=3+2+4+4=13,14号=14号+15号+16号=2+4+4=10

如果不指定ROWS BETWEEN,默认为从起点到当前行;

如果不指定ORDER BY,则将分组内所有值累加;
关键是理解ROWS BETWEEN含义,也叫做WINDOW子句:
PRECEDING:往前
FOLLOWING:往后
CURRENT ROW:当前行
UNBOUNDED:起点,

  UNBOUNDED PRECEDING 表示从前面的起点,

  UNBOUNDED FOLLOWING:表示到后面的终点

–其他AVG,MIN,MAX,和SUM用法一样。

 

玩一玩AVG

 

查询语句

select    cookieid,    createtime,    pv,    avg(pv) over (partition by cookieid order by createtime rows between unbounded preceding and current row) as pv1, -- 默认为从起点到当前行   avg(pv) over (partition by cookieid order by createtime) as pv2, --从起点到当前行,结果同pv1   avg(pv) over (partition by cookieid) as pv3, --分组内所有行   avg(pv) over (partition by cookieid order by createtime rows between 3 preceding and current row) as pv4, --当前行+往前3行   avg(pv) over (partition by cookieid order by createtime rows between 3 preceding and 1 following) as pv5, --当前行+往前3行+往后1行   avg(pv) over (partition by cookieid order by createtime rows between current row and unbounded following) as pv6  --当前行+往后所有行from cookie1;

 

查询结果

 

玩一玩MIN

 

查询语句

select    cookieid,    createtime,    pv,    min(pv) over (partition by cookieid order by createtime rows between unbounded preceding and current row) as pv1, -- 默认为从起点到当前行   min(pv) over (partition by cookieid order by createtime) as pv2, --从起点到当前行,结果同pv1   min(pv) over (partition by cookieid) as pv3, --分组内所有行   min(pv) over (partition by cookieid order by createtime rows between 3 preceding and current row) as pv4, --当前行+往前3行   min(pv) over (partition by cookieid order by createtime rows between 3 preceding and 1 following) as pv5, --当前行+往前3行+往后1行   min(pv) over (partition by cookieid order by createtime rows between current row and unbounded following) as pv6  --当前行+往后所有行from cookie1;

 

 

查询结果 

 

玩一玩MAX

 

查询语句

select    cookieid,    createtime,    pv,    max(pv) over (partition by cookieid order by createtime rows between unbounded preceding and current row) as pv1, -- 默认为从起点到当前行   max(pv) over (partition by cookieid order by createtime) as pv2, --从起点到当前行,结果同pv1   max(pv) over (partition by cookieid) as pv3, --分组内所有行   max(pv) over (partition by cookieid order by createtime rows between 3 preceding and current row) as pv4, --当前行+往前3行   max(pv) over (partition by cookieid order by createtime rows between 3 preceding and 1 following) as pv5, --当前行+往前3行+往后1行   max(pv) over (partition by cookieid order by createtime rows between current row and unbounded following) as pv6  --当前行+往后所有行from cookie1;

 

 

查询结果

 

转载于:https://www.cnblogs.com/h-kang/p/10916727.html

你可能感兴趣的文章
cudaMalloc和cudaMallocPitch
查看>>
如何打卡后缀为3ds的文件
查看>>
POJ2524 并查集
查看>>
boost asio resolver
查看>>
<转>.h和.cpp文件的区别
查看>>
[转]svn常用命令
查看>>
Swing学习1——总体概述
查看>>
nginx 注释配置及详解
查看>>
QCustomplot(一) 能做什么事
查看>>
vue1.0和vue2.0生命周期----整理一
查看>>
Could not load the Tomcat server configuration at \Servers\Tomcat v7.0 Server at localhost-config
查看>>
对象的成员的初始化
查看>>
zbb20180710 maven Failed to read artifact descriptor--maven
查看>>
关于Webapp的注意事项
查看>>
使用JDBC进行数据库的事务操作(2)
查看>>
HDU 3966 Aragorn's Story (树链剖分+线段树)
查看>>
MIME协议(三) -- MIME邮件的组织结构
查看>>
javascript:设置URL参数的方法,适合多条件查询
查看>>
javascript获取URL查询字符串
查看>>
大型网站架构演化(二)——应用服务和数据服务分离
查看>>