hive有关函数
1.窗口函数
2015年4月份购买过的顾客及总人数
select distinct name,count(1) over() as cnt from test_window_yf
where substr(orderdate,1,7)='2015-04';
select name,count(1) over() as cnt from test_window_yf
where substr(orderdate,1,7)='2015-04' group by name;
顾客的购买明细及月购买总额
将cost按照月进行累加
//默认从起始行到当前行
select name,orderdate,cost,sum(cost) over(partition by month(orderdate) order by orderdate) from test_window_yf;
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 --当前行及后面所有行
select name,orderdate,cost,
from test_window_yf;
name orderdate cost sample1 sample2 sample3 sample4 sample5 sample6 sample7
jack 2015-01-01 10 661 176 10 10 10 56 176
jack 2015-01-05 46 661 176 56 56 56 111 166
jack 2015-01-08 55 661 176 111 111 101 124 120
jack 2015-02-03 23 661 176 134 134 78 120 65
jack 2015-04-06 42 661 176 176 176 65 65 42
mart 2015-04-08 62 661 299 62 62 62 130 299
mart 2015-04-09 68 661 299 130 130 130 205 237
mart 2015-04-11 75 661 299 205 205 143 237 169
mart 2015-04-13 94 661 299 299 299 169 169 94
neil 2015-05-10 12 661 92 12 12 12 92 92
neil 2015-06-12 80 661 92 92 92 92 92 80
tony 2015-01-02 15 661 94 15 15 15 44 94
tony 2015-01-04 29 661 94 44 44 44 94 79
tony 2015-01-07 50 661 94 94 94 79 79 50
select name,orderdate,cost,
ntile(4) over() as sample1 , --全局数据切片
ntile(4) over(partition by name), -- 按照name进行分组,在分组内将数据切成3份
ntile(4) over(order by cost),--全局按照cost升序排列,数据切成3份
ntile(4) over(partition by name order by cost ) --按照name分组,在分组内按照cost升序排列,数据切成3份
from test_window_yf;
2.高级聚合函数
grouping sets / cube / rollup
grouping__id
2015-03,2015-03-10,cookie1
2015-03,2015-03-10,cookie5
2015-03,2015-03-12,cookie7
2015-04,2015-04-12,cookie3
2015-04,2015-04-13,cookie2
2015-04,2015-04-13,cookie4
2015-04,2015-04-16,cookie4
2015-03,2015-03-10,cookie2
2015-03,2015-03-10,cookie3
2015-04,2015-04-12,cookie5
2015-04,2015-04-13,cookie6
2015-04,2015-04-15,cookie3
2015-04,2015-04-15,cookie2
2015-04,2015-04-16,cookie1
CREATE TABLE sospdm.test_function_yf (
month STRING,
day STRING,
cookieid STRING
) ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
stored as textfile;
GROUPING SETS
在一个GROUP BY查询中,根据不同的维度组合进行聚合,等价于将不同维度的GROUP BY结果集进行 UNION ALL
SELECT
month,
day,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM sospdm.test_function_yf
GROUP BY month,day
GROUPING SETS ((month,day),day);
--
ORDER BY GROUPING__ID;
--GROUPING__ID,表示结果属于哪一个分组集合。
month day uv GROUPING__ID
2015-03 NULL 5 1
2015-04 NULL 6 1
NULL 2015-03-10 4 2
NULL 2015-03-12 1 2
NULL 2015-04-12 2 2
NULL 2015-04-13 3 2
NULL 2015-04-15 2 2
NULL 2015-04-16 2 2
<=>
SELECT month,NULL,COUNT(DISTINCT cookieid) AS uv,1 AS GROUPING__ID FROM lxw1234 GROUP BY month
UNION ALL
SELECT NULL,day,COUNT(DISTINCT cookieid) AS uv,2 AS GROUPING__ID FROM lxw1234 GROUP BY day
SELECT
month,
day,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM sospdm.test_function_yf
GROUP BY month,day
GROUPING SETS ((month,day),(day))
ORDER BY GROUPING__ID;
2015-04 NULL 6 1
2015-03 NULL 5 1
NULL 2015-03-10 4 2
NULL 2015-04-16 2 2
NULL 2015-04-15 2 2
NULL 2015-04-13 3 2
NULL 2015-04-12 2 2
NULL 2015-03-12 1 2
2015-04 2015-04-16 2 3
2015-04 2015-04-12 2 3
2015-04 2015-04-13 3 3
2015-03 2015-03-12 1 3
2015-03 2015-03-10 4 3
2015-04 2015-04-15 2 3
cube:
根据GROUP BY的维度的所有组合进行聚合。
SELECT
month,
day,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM sospdm.test_function_yf
GROUP BY month,day
WITH CUBE
ORDER BY GROUPING__ID;
<=>
SELECT
month,
day,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM sospdm.test_function_yf
GROUP BY month,day
grouping sets((month,day),month,day,())
ORDER BY GROUPING__ID;
NULL NULL 7 0 --区别
2015-03 NULL 5 1
2015-04 NULL 6 1
NULL 2015-04-16 2 2
NULL 2015-04-15 2 2
NULL 2015-04-13 3 2
NULL 2015-04-12 2 2
NULL 2015-03-12 1 2
NULL 2015-03-10 4 2
2015-04 2015-04-12 2 3
2015-04 2015-04-16 2 3
2015-03 2015-03-12 1 3
2015-03 2015-03-10 4 3
2015-04 2015-04-15 2 3
2015-04 2015-04-13 3 3
ROLLUP
是CUBE的子集,以最左侧的维度为主,从该维度进行层级聚合。
比如,以month维度进行层级聚合:
SELECT
month,
day,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM lxw1234
GROUP BY month,day
WITH ROLLUP
ORDER BY GROUPING__ID;
month day uv GROUPING__ID
---------------------------------------------------
NULL NULL 7 0
2015-03 NULL 5 1
2015-04 NULL 6 1
2015-03 2015-03-10 4 3
2015-03 2015-03-12 1 3
2015-04 2015-04-12 2 3
2015-04 2015-04-13 3 3
2015-04 2015-04-15 2 3
2015-04 2015-04-16 2 3
--把month和day调换顺序,则以day维度进行层级聚合:
SELECT
day,
month,
COUNT(DISTINCT cookieid) AS uv,
GROUPING__ID
FROM lxw1234
GROUP BY day,month
WITH ROLLUP
ORDER BY GROUPING__ID;
day month uv GROUPING__ID
------------------------------------
NULL NULL 7 0
2015-04-13 NULL 3 1
2015-03-12 NULL 1 1
2015-04-15 NULL 2 1
2015-03-10 NULL 4 1
2015-04-16 NULL 2 1
2015-04-12 NULL 2 1
2015-04-12 2015-04 2 3
2015-03-10 2015-03 4 3
2015-03-12 2015-03 1 3
2015-04-13 2015-04 3 3
2015-04-15 2015-04 2 3
2015-04-16 2015-04 2 3
------
二、日期函数
1.日期函数 to_date(string expr)
返回类型:string
描述:返回时间字符串日期部分
to_date(expr) - Extracts the date part of the date or datetime expression expr
实例:
hive> select to_date('2014-09-16 15:50:08.119') from default.dual;
2014-09-16
2.年份函数 year(string expr)
返回类型:int
描述:返回时间字符串年份数字
year(date) - Returns the year of date
实例:
hive> select year('2014-09-16 15:50:08.119') from default.dual;
2014
3.月份函数 month(string expr)
返回类型:int
描述:返回时间字符串月份数字
month(date) - Returns the month of date
实例:
hive> select month('2014-09-16 15:50:08.119') from default.dual;
09
4.天函数 day(string expr)
返回类型:int
描述:返回时间字符串的天
day(date) - Returns the date of the month of date
实例:
hive> select day('2014-09-16 15:50:08.119') from default.dual;
16
5.小时函数 hour(string expr)
返回类型:int
描述:返回时间字符串小时数字
hour(date) - Returns the hour of date
实例:
hive> select hour('2014-09-16 15:50:08.119') from default.dual;
15
6.分钟函数 hour(string expr)
返回类型:int
描述:返回时间字符串分钟数字
minute(date) - Returns the minute of date
实例:
hive> select minute('2014-09-16 15:50:08.119') from default.dual;
50
7.秒函数 second(string expr)
返回类型:int
描述:返回时间字符串分钟数字
second(date) - Returns the second of date
实例:
hive> select second('2014-09-16 15:50:08.119') from default.dual;
08
8.日期增加函数 date_add(start_date, num_days)
返回类型:string
描述:返回增加num_days 天数的日期(负数则为减少)
date_add(start_date, num_days) - Returns the date that is num_days after start_date.
实例:
hive>select date_add('2014-09-16 15:50:08.119',10) from default.dual;
2014-09-26
hive>select date_add('2014-09-16 15:50:08.119',-10) from default.dual;
2014-09-06
9.日期减少函数 date_sub(start_date, num_days)
返回类型:string
描述:返回num_days 天数之前的日期(负数则为增加)
date_sub(start_date, num_days) - Returns the date that is num_days before start_date.
实例:
hive>select date_sub('2014-09-16 15:50:08.119',10) from default.dual;
2014-09-06
hive>select date_sub('2014-09-16 15:50:08.119',-10) from default.dual;
2014-09-26
10.周期函数 weekofyear(start_date, num_days)
返回类型:int
描述:返回当前日期位于本年的周期 一周一个周期
weekofyear(date) - Returns the week of the year of the given date. A week is considered to start on a Monday and week 1 is the first week with >3 days.
实例:
hive>select weekofyear('2014-09-16 15:50:08.119') from default.dual;
38
11.日期比较函数 weekofyear(start_date, num_days)
返回类型:string
描述:返回2个时间的日期差
datediff(date1, date2) - Returns the number of days between date1 and date2
date1-date2
实例:
hive>select datediff('2014-09-16 15:50:08.119','2014-09-15') from default.dual;
1
hive有关函数的更多相关文章
- Hive自定义函数的学习笔记(1)
前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...
- hive -- 自定义函数和Transform
hive -- 自定义函数和Transform UDF操作单行数据, UDAF:聚合函数,接受多行数据,并产生一个输出数据行 UDTF:操作单个数据 使用udf方法: 第一种: add jar xxx ...
- hive 数值计算函数
Hive数值计算函数 (1)round(45.666,2)作用:四舍五入,保留2位小数 ceil(45.6) 作用:向上取整 floor(45.6) 作用:向下取整 (2)rand() ...
- Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- Hadoop3集群搭建之——hive添加自定义函数UDTF
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- Hadoop3集群搭建之——hive添加自定义函数UDF
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- hive自定义函数(UDF)
首先什么是UDF,UDF的全称为user-defined function,用户定义函数,为什么有它的存在呢?有的时候 你要写的查询无法轻松地使用Hive提供的内置函数来表示,通过写UDF,Hive就 ...
- 第3节 hive高级用法:13、hive的函数
4.2.Hive参数配置方式 Hive参数大全: https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties 开 ...
- Hive常用函数的使用
Hive常用函数的使用 文章作者:foochane 原文链接:https://foochane.cn/article/2019062501.html 1 基本介绍 1.1 HIVE简单介绍 Hive ...
- Hive concat函数连接后结果为null
Hive concat函数连接后结果为null concat函数是用来连接字符串的 使用示例: select concat('Hello','World','Java'); 运行结果: 最近我们在做需 ...
随机推荐
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- js小方法积累,将一个数组按照n个一份,分成若干数组
// 把一个数组按照一定长度分割成若干数组 function group(array, subGroupLength) { let index = 0; let newArray = []; whil ...
- ES6学习路上的小学生,promise处理异步操作,简易原始起步之用。先能用,再深究!
ES6的promise对象,让我们更容易的处理这样的需求:执行完一个方法以后,再去执行下一个方法. 理解尚浅之时,先用于项目之中. var promise1 = new Promise(functio ...
- js new一个函数和直接调用函数的区别
用new和调用一个函数的区别:如果函数返回值是一个值类型(Number.String.Boolen)时,new函数将会返回这个函数的实例对象,而如果这个函数的返回值是一个引用类型(Object.Arr ...
- day14 迭代器 生成器 面向过程思想
"" 迭代器 什么是迭代器(iterator) 器指的某种工具, 迭代指的是更新换代的过程,例如应用程序的版本更新从1.0 变成 1.1 再1.2 迭代的目的是要根据上一个结果,产 ...
- 【转载】中文输入法下onKeyPress不能触发的问题
onKeypress---->oninput https://segmentfault.com/a/1190000008820968
- 五.Bash Shell编程基础入门实战
知识回顾 运行脚本我们一般用sh 不用单独去加执行权限 OLDBOY=10只适用当前环境 局部变量 export OLDBOY把它设置为临时的环境变量应为已经=10了所以不用export OLDBOY ...
- mysql 文件导入
load data infile 文件路径 into table 表 fields terminated by ',' lines terminated '\n'
- node.js 框架express有关于router的运用
1.express 路由入门 const express = require('express'); let server = express(); server.listen(8087); //用户 ...
- Nginx详解五:Nginx基础篇之HTTP请求
http请求 如今的http请求已经不是每一次请求都进行一次三次握手,可以在请求与相应之后,客户端和服务端不断的发送FIN和ACK包来保持连接的状态,即:长连接 HTTP请求建立在一次TCP连接基础上 ...