IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> hive-分析、窗口函数的使用 -> 正文阅读

[大数据]hive-分析、窗口函数的使用

5-1:sum,avg,min,max窗口内聚合分析

over (partition by col1 order by col2 rows between unbounded[n] preceding and current row[n following])

PRECEDING:往前
FOLLOWING:往后
CURRENT ROW:当前行
UNBOUNDED:起点,
UNBOUNDED PRECEDING 表示从前面的起点,
UNBOUNDED FOLLOWING:表示到后面的终点

select

id

,order_num

,sum(order_num) over (partition by id)? `分组内所有行`

,sum(order_num) over (partition by id order by dt) `从起点到当前行`

,sum(order_num) over (partition by id order by dt rows between unbounded preceding and current row) `默认为从起点到当前行`

,sum(order_num) over (partition by id order by dt rows between 3 preceding and current row) `当前行+往前3行`

,sum(order_num) over (partition by id order by dt rows between current row and 3 following) `当前行+往后3行`

,sum(order_num) over (partition by id order by dt rows between 3 preceding and 1 following) `当前行+往前3行+往后1行`

,sum(order_num) over (partition by id order by dt rows between current row and unbounded following) `当前行+往后所有行`

,max(order_num) over (partition by id order by dt rows between 3 preceding and 1 following) `当前行+往前3行+往后1行里面取最大`

,count(1) over (partition by id order by dt rows between 3 preceding and 1 following)??? `当前行+往前3行+往后1行里面计数`

,avg(order_num) over (partition by id order by dt rows between 3 preceding and 1 following)??? `当前行+往前3行+往后1行里面求平均数`

,first_value(order_num) over (partition by id order by dt rows between 3 preceding and 1 following) `当前行+往前3行+往后1行第1个值`

,last_value(order_num) over (partition by id order by dt rows between 3 preceding and 1 following ) `当前行+往前3行+往后1行第最后1个值`

?from tmp.tmp_clq_20190816

5-2:Lead、leg(适用于环比,周环比,月环比,连续登里等,无需多表关联)

select
id
,dt
,LAG(dt,1,'1970-01-01 00:00:00') over (partition by id order by dt) `向上取1行`
,LAG(dt,3,'1970-01-01 00:00:00') over (partition by id order by dt) `向上取3行`
,lead(dt,1,'1970-01-01 00:00:00') over (partition by id order by dt) `向下取1行`
,lead(dt,3,'1970-01-01 00:00:00') over (partition by id order by dt) `向下取1行`
from tmp.tmp_clq_20190816

5-3:–CUME_DIST 、PERCENT_RANK?

–CUME_DIST 小于等于当前值的行数/分组内总行数

–PERCENT_RANK 分组内当前行的RANK值-1/分组内总行数-1

select
id
,order_num
,CUME_DIST()OVER(ORDER BY order_num) `取所有订单数的占比情况`
,CUME_DIST()OVER(PARTITION BY id ORDER BY order_num) `按照id分组内取订单数的占比情况`
,PERCENT_RANK() OVER(PARTITION BY id ORDER BY order_num) `分组内当前行的RANK值-1/分组内总行数-1`
,NTILE(2) OVER(PARTITION BY id ORDER BY order_num) `分组内将数据分成2片`
,NTILE(3) OVER(PARTITION BY id ORDER BY order_num) `分组内将数据分成3片`
,NTILE(4) OVER(PARTITION BY id ORDER BY order_num) `分组内将数据分成4片`
from tmp.tmp_clq_20190816

5-4:GROUPING SETS

在一个GROUP BY查询中,根据不同的维度组合进行聚合,等价于将不同维度的GROUP BY结果集进行UNION ALL

select
id,biz ,sum(order_num)
,GROUPING__ID
from tmp.tmp_clq_20190816
group by
id,biz
GROUPING SETS (
(id,biz),id,biz
) order by GROUPING__ID

5-5:CUBE

根据GROUP BY的维度的所有组合进行聚合

select
id ,biz ,sum(order_num)
,GROUPING__ID
from tmp.tmp_clq_20190816
group by
id,biz
WITH CUBE
order by GROUPING__ID

5-6:ROLLUP

是CUBE的子集,以最左侧的维度为主,从该维度进行层级聚合

select
id,biz
,sum(order_num)
,GROUPING__ID
from tmp.tmp_clq_20190816
group by
id,biz
WITH ROLLUP
order by GROUPING__ID

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-03-21 20:57:41  更:2022-03-21 20:58:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 6:53:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码