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有关函数的更多相关文章

  1. Hive自定义函数的学习笔记(1)

    前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...

  2. hive -- 自定义函数和Transform

    hive -- 自定义函数和Transform UDF操作单行数据, UDAF:聚合函数,接受多行数据,并产生一个输出数据行 UDTF:操作单个数据 使用udf方法: 第一种: add jar xxx ...

  3. hive 数值计算函数

    Hive数值计算函数 (1)round(45.666,2)作用:四舍五入,保留2位小数 ceil(45.6) 作用:向上取整         floor(45.6) 作用:向下取整 (2)rand() ...

  4. Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  5. Hadoop3集群搭建之——hive添加自定义函数UDTF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  6. Hadoop3集群搭建之——hive添加自定义函数UDF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  7. hive自定义函数(UDF)

    首先什么是UDF,UDF的全称为user-defined function,用户定义函数,为什么有它的存在呢?有的时候 你要写的查询无法轻松地使用Hive提供的内置函数来表示,通过写UDF,Hive就 ...

  8. 第3节 hive高级用法:13、hive的函数

    4.2.Hive参数配置方式 Hive参数大全: https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties 开 ...

  9. Hive常用函数的使用

    Hive常用函数的使用 文章作者:foochane  原文链接:https://foochane.cn/article/2019062501.html 1 基本介绍 1.1 HIVE简单介绍 Hive ...

  10. Hive concat函数连接后结果为null

    Hive concat函数连接后结果为null concat函数是用来连接字符串的 使用示例: select concat('Hello','World','Java'); 运行结果: 最近我们在做需 ...

随机推荐

  1. 缺失dll的问题

    不小心运行一下什么程序就会出现缺失xxx.dll的问题,太烦了,遇到好多,一直没有记录.现在开始记录,以便日后查看~ 1. api-ms-win-crt-runtime-l1-1-0.dll 64位系 ...

  2. Confluence 6 有关 AD 的一些特殊说明

    当应用程序对使用 Active Directory (AD) 的 LDAP 服务器进行同步的时候,同步的任务只对 LDAP 最近修改的数据进行同步而不是对整个数据库进行同步.因为是增量同步,在第一次完 ...

  3. 新增 修改,对xx名字或者其他属性做校验判断是否存在

    需求描述:页面输入完xxName和xx编码,点击提交,根据两项内容做重复校验(就是看看数据库里有木有相同的) 解决思路:把这两个东西作为查询条件去查,查到有记录,提示已存在,就不执行新增或者修改操作. ...

  4. linux 源码安装PHP

    解压: 解压完: configure: configure成功: make: make完成: 安装完成!!! 测试: 需要./bin/php来运行php 想要任何目录输入PHP就能使用php 方法一: ...

  5. bzoj 2301

    一道莫比乌斯反演入门题. 首先观察题目要求:的数对数 首先可以发现,这个东西同时有上界和下界,所以并不是很容易计算 那么我们变下形,可以看到:原式= 是不是清晰很多了?(当然没有!) 不,这一步很重要 ...

  6. CF979E

    非常好的dp,非常考dp的能力 很显然是个计数问题,那么很显然要么是排列组合,要么是递推,这道题很显然递推的面更大一些. 那么我们来设计一下状态: 设状态f[i][j][k][p]表示目前到了第i个点 ...

  7. 论文阅读笔记三十四:DSSD: Deconvolutiona lSingle Shot Detector(CVPR2017)

    论文源址:https://arxiv.org/abs/1701.06659 开源代码:https://github.com/MTCloudVision/mxnet-dssd 摘要 DSSD主要是向目标 ...

  8. 步步为营103-ZTree 二级联动

    1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...

  9. linux 系统备份和恢复

    Linux不像windows,它不限制根用户存取任何东西,因此,你完全可以把一个分区上每一个的文件放入一个TAR文件中. 使用root用户切换到根目录 然后,使用下面的命令备份完整的系统: tar c ...

  10. vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法

    错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives.Renders the ...