【HIVE】(3)联合查询join、时间戳函数、字符串函数
数据
t_join1.txt
1,a,1
2,b,2
3,c,4
t_join2.txt
1,2a
2,2b
3,2c
建表、导入:
create table t_join1(id int, name string, cid int) row format delimited fields terminated by ",";
create table t_join2(id int, name string) row format delimited fields terminated by ",";
load data local inpath "/root/example/hive/data/t_join1.txt" into table t_join1;
load data local inpath "/root/example/hive/data/t_join2.txt" into table t_join2;
join,多表联合查询
join:只输出on条件相等的行
select * from t_join1 t1 join t_join2 t2 on t1.cid=t2.id;
等同于:
select * from t_join1 t1 inner join t_join2 t2 on t1.cid=t2.id;
select * from t_join1 t1, t_join2 t2 where t1.cid=t2.id;
left join,输出on条件相等的行 + 左表剩余的行(这些行对应右表的字段使用NULL)
select * from t_join1 t1 left join t_join2 t2 on t1.cid=t2.id;
right join,输出on条件相等的行 + 右表剩余的行(这些行对应左表的字段使用NULL)
select * from t_join1 t1 right join t_join2 t2 on t1.cid=t2.id;
full join,输出左右两表所有行,on条件不满足的行,使用NULL
select * from t_join1 t1 full join t_join2 t2 on t1.cid=t2.id;
built-in function,内置函数
1.查看系统自带的函数
hive> show functions;
2.显示自带的函数的用法
hive> desc function year;
3.详细显示自带的函数的用法
hive> desc function extended year;
* 数值函数:
round(DOUBLE a):四舍五入取整;
round(DOUBLE a, INT d):指定小数点位数;
rand():0~1之间的随机小数;
select round(rand()*a+b):取[b, a+b]之间的整数;
* 日期函数:
- 当前时间:
current_date
current_timestamp
- 时间戳函数:
from_unixtime(bigint unixtime[, string format]):时间戳转换为时间;
unix_timestamp():获取当前时间戳;
unix_timestamp(string date):时间转换为时间戳;
- 分别获取年、月、日:
year(string date)
quarter(date/timestamp/string)
month(string date)
day(string date) dayofmonth(date)
hour(string date)
minute(string date)
second(string date)
weekofyear(string date)
- 时间间隔:
datediff(string enddate, string startdate):两段时间相隔的天数;
date_add(date/timestamp/string startdate, tinyint/smallint/int days):增加天数;
date_sub(date/timestamp/string startdate, tinyint/smallint/int days):减少天数;
* 条件函数:
if(boolean testCondition, T valueTrue, T valueFalseOrNull)
isnull( a )
isnotnull ( a )
nvl(T value, T default_value):替换空值;
COALESCE(T v1, T v2, ...):返回第一个非空值;
重难点: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END:该函数可以将多行转换为多行多列;
- 增加yuwen和shuxue列:
select * , case course when "yuwen" then score else 0 end as yuwen, case course when "shuxue" then score else 0 end as shuxue from t_course;
- 使用max分组:
select sid, max(yuwen) as yuwen, max(shuxue) as shuxue from (select * , case course when "yuwen" then score else 0 end as yuwen, case course when "shuxue" then score else 0 end as shuxue from t_course) t group by sid;
- 获取语文成绩比数学成绩好的学生:
select * from (select sid, max(yuwen) as yuwen, max(shuxue) as shuxue from (select * , case course when "yuwen" then score else 0 end as yuwen, case course when "shuxue" then score else 0 end as shuxue from t_course) t group by sid) t where yuwen>shuxue;
还有另一种方法,可以完成上面任务,提示:使用collect_set(),大家可以先想想思路。
CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
* 字符串函数:
concat(string|binary A, string|binary B...):字符串连接
concat_ws(string SEP, string A, string B...):使用指定字符连接;
substr(string|binary A, int start, int len):获取子字符串;
substring(string|binary A, int start, int len):同上;
trim(string A):去除两边空格;
lower(string A) lcase(string A) upper(string A) ucase(string A):大小写转换;
split(string str, string pat):字符串分割为数组;
str_to_map(text[, delimiter1, delimiter2]):字符串转换为map;
【HIVE】(3)联合查询join、时间戳函数、字符串函数的更多相关文章
- javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数
javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个 ...
- 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数
一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x) 返回大于或等于x的最小整数 SELEC ...
- Sass函数--字符串函数
Sass的函数简介在 Sass 中除了可以定义变量,具有 @extend.%placeholder 和 mixins 等特性之外,还自备了一系列的函数功能.其主要包括: ● 字符串函数 ● 数字函数 ...
- ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数
原文:ylb:SQLServer常用系统函数-字符串函数.配置函数.系统统计函数 ylbtech-SQL Server:SQL Server-SQLServer常用系统函数 -- ========== ...
- mssql 系统函数-字符串函数专题--字符串函数大全
mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...
- php常用函数——字符串函数
php常用函数——字符串函数
- Linux下常用函数-字符串函数
inux下常用函数-字符串函数 atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib ...
- EF 表联合查询 join
有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...
- sql server 系统常用函数:聚合函数 数学函数 字符串函数 日期和时间函数和自定义函数
一.系统函数 1.聚合函数 聚合函数常用于GROUP BY子句,在SQL Server 2008提供的所有聚合函数中,除了COUNT函数以外,聚合函数都会忽略空值AVG.COUNT.COUNT_BIG ...
随机推荐
- JS做类型检测到底有几种方法?看完本文就知道了!
JS有很多数据类型,对于不同数据类型的识别和相互转换也是面试中的一个常考点,本文主要讲的就是类型转换和类型检测. 数据类型 JS中的数据类型主要分为两大类:原始类型(值类型)和引用类型.常见的数据类型 ...
- 【Scala】关于集合的各种知识点
目录 映射Map 不可变Map 概述 操作实例 可变Map 概述 操作实例 Map的遍历 for循环遍历 格式 操作实例 模式匹配遍历 格式 操作实例 Tuple 元祖 概述 定义格式 获取元素方法 ...
- zabbix部署与配置
zabbix部署与配置 1.zabbix的web界面是基于php开发,所以创建lnmp环境来支持web界面的访问 yum install nginx php php-devel php-mysql p ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 这是一篇致力于解决Linux小白无法安装tunctl工具的文章
计算机网络——搭建Linux下的实验环境并成功安装tunctl 各位如果是来解决安装tunctl的问题的,请直接到2.2部分的内容. 这个学期终于迎来了计算机网络这门课程,也终于能够进一步了解让我无数 ...
- XShell 评估到期
刚刚打开XShell弹出”评估到期“,点击确定后自动打开中文官网,得购买后才能使用. 当初下载的时候没留意到会有这一天.. 手头拮据的朋友可以通过下面方法绕过: 删除XShell. 到英文官网下载页找 ...
- intellij tomcat
VMOption -server -XX:PermSize=128M -XX:MaxPermSize=256m
- 最小割经典题(两个点依附在一起的情况)poj3469
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 25099 Accepted: 10866 ...
- 基于SpringCloud分布式架构
基于SpringCloud分布式架构 为什么要使用分布式架构 Spring Cloud 专注于提供良好的开箱即用经验的典型用例和可扩展性机制覆盖 分布式/版本化配置 服务注册和发现 路由 Servic ...
- A+B Coming(hdu1720)
思考:十六进制的输入->%x,定义时用int.要变成十进制输出,直接在输出时用->%d. #include<stdio.h> int main() { int A,B; cha ...