Hive 之 函数 01-常用查询函数(一)

本文介绍了Hive中的常用查询函数,包括空字段赋值的NVL函数,时间类函数如date_format、date_add、date_sub和datediff,case when语句,以及行转列和列转行的函数如CONCAT、CONCAT_WS、COLLECT_SET、EXPLODE和lateral view的实际应用示例。
摘要由CSDN通过智能技术生成

欢迎大家扫码关注我的微信公众号:
数据之恋

一、 空字段赋值

NVL 函数: 给值为 Null 的函数赋值, 它的格式是 NVL(string1, replace_with), 它的功能是如果 string1 的值不为 Null, 则返回 string1 的值, 否则, 将返回 replace_with 的值, 如果 replace_with 的值也为 Null, 则返回 Null。

hive (default)> select comm from emp;
OK
comm
NULL
300.0
500.0
NULL
1400.0
NULL
NULL
NULL
NULL
0.0
NULL
NULL
NULL
NULL
Time taken: 0.892 seconds, Fetched: 14 row(s)

hive (default)> select NVL(comm, '--') from emp;
OK
_c0
--
300.0
500.0
--
1400.0
--
--
--
--
0.0
--
--
--
--
Time taken: 0.072 seconds, Fetched: 14 row(s)

【注】NVL 函数的第二个参数也可以是表中的某个字段:

hive (default)> select empno, nvl(comm, empno) from emp;
OK
empno	_c1
7369	7369.0
7499	300.0
7521	500.0
7566	7566.0
7654	1400.0
7698	7698.0
7782	7782.0
7788	7788.0
7839	7839.0
7844	0.0
7876	7876.0
7900	7900.0
7902	7902.0
7934	7934.0
Time taken: 0.112 seconds, Fetched: 14 row(s)

二、 时间类

2.1 date_format: 格式化时间

hive(default)> select date_format('1990-02-12', 'yyyy-MM-dd HH:mm:ss');
OK
_c0
1990-02-12 00:00:00
Time taken: 0.471 seconds, Fetched: 1 row(s)

2.2 date_add: 时间跟天数相加

hive (default)> select date_add('2020-02-26', 4);
OK
_c0
2020-03-01
Time taken: 0.154 seconds, Fetched: 1 row(s)

hive (default)> select date_add('2020-02-26', -4);
OK
_c0
2020-02-22
Time taken: 0.079 seconds, Fetched: 1 row(s)

2.3 date_sub: 时间跟天数相减(跟 date_add 记一个就行)

hive (default)> select date_sub('2020-02-26', 4);
OK
_c0
2020-02-22
Time taken: 0.103 seconds, Fetched: 1 row(s)

hive (default)> select date_sub('2020-02-26', -4);
OK
_c0
2020-03-01
Time taken: 0.069 seconds, Fetched: 1 row(s)

2.4 datediff: 两个时间相减

第一个参数减去第二个参数。

hive(default)> select datediff('2020-03-03', '2020-02-28');
OK
_c0
4
Time taken: 0.125 seconds, Fetched: 1 row(s)

hive (default)> select datediff('2020-02-28', '2020-03-03');
OK
_c0
-4
Time taken: 0.057 seconds, Fetched: 1 row(s)

2.5 注意:

以上关于时间的计算只接受中横线作为时间的连接符, / 作为连接符的时间会得出 Null 的结果:

hive (default)> select date_add(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值