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. hive学习03-求一年中的最大温度

    知识点: substr.concat函数的使用: row_number() over(distribute by year sort by temp desc)  #按照年分组,按照tmp去排序  需 ...

  2. Swift 学习- 03 -- 基本运算符

    // 运算符是检查,改变,合并值的特殊符号或短语, // swift 支持大部分标准的 C语言的运算符, 且改进许多特性来减少常规编码错误. /* 如: 赋值符 (=) 不返回值, 以防止把想要判断相 ...

  3. day07 元组类型 字典类型 集合

    元组:元组就是一个不可变的列表 1.用途:当我们需要记录多个同种属性的值,并且只有读的需求,没有改的需求,应该用元组. 2.定义方式:在()内用逗号分隔开多个任意类型的元素 t=(‘egon’)#注意 ...

  4. bzoj1195 神奇的ac自动机+状态压缩dp

    /* 难的不是ac自动机,是状态压缩dp 之前做了一两题类似题目,感觉理解的还不够透彻 */ #include<iostream> #include<cstdio> #incl ...

  5. mybatis常见错误总结

    1. 现象:mybatis xml文件中查询的返回类型写成list或java.util.List时,执行sql时报 java.lang.UnsupportedOperationException错误. ...

  6. Windows Internals 笔记——错误处理

    1.Windows函数检测到错误时,会使用一种名为“线程本地存储区”的机制将相应的错误代码与“主调线程”关联到一起.这种机制使得不同的线程能独立运行,不会出现相互干扰对方的错误代码的情况. 2.Get ...

  7. 饮冰三年-人工智能-linux-08 软件包管理(Python的安装)

    1:软件包存放的位置 media/CentOS_6.9_Final/Packages文件夹下 2.RPM就是Red Hat Package Manger(红帽软件包管理工具)的缩写. 2.1 常用的命 ...

  8. 常见的爬虫分析库(1)-Python3中Urllib库基本使用

    原文来自:https://www.cnblogs.com/0bug/p/8893677.html 什么是Urllib? Python内置的HTTP请求库 urllib.request          ...

  9. 配置frp

    一.下载 下载地址:https://github.com/fatedier/frp/releases 下载linux_amd64的,如果是32位系统就下载linux_386 二.安装 有公网IP的服务 ...

  10. mysql把查询结果集插入到表理

    把表B的内容插入到表A INSERT INTO 1111_0 SELECT*FROM report_0 把查询结果集插入到表中 insert into A(a,b,c) select from B(a ...