数据
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、时间戳函数、字符串函数的更多相关文章

  1. javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数

    javascript函数一共可分为五类:    ·常规函数    ·数组函数    ·日期函数    ·数学函数    ·字符串函数    1.常规函数    javascript常规函数包括以下9个 ...

  2. 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数

    一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x)  返回大于或等于x的最小整数 SELEC ...

  3. Sass函数--字符串函数

    Sass的函数简介在 Sass 中除了可以定义变量,具有 @extend.%placeholder 和 mixins 等特性之外,还自备了一系列的函数功能.其主要包括: ● 字符串函数 ● 数字函数 ...

  4. ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数

    原文:ylb:SQLServer常用系统函数-字符串函数.配置函数.系统统计函数 ylbtech-SQL Server:SQL Server-SQLServer常用系统函数 -- ========== ...

  5. mssql 系统函数-字符串函数专题--字符串函数大全

    mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...

  6. php常用函数——字符串函数

    php常用函数——字符串函数

  7. Linux下常用函数-字符串函数

    inux下常用函数-字符串函数 atof(将字符串转换成浮点型数)  相关函数   atoi,atol,strtod,strtol,strtoul 表头文件   #include <stdlib ...

  8. EF 表联合查询 join

    有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...

  9. sql server 系统常用函数:聚合函数 数学函数 字符串函数 日期和时间函数和自定义函数

    一.系统函数 1.聚合函数 聚合函数常用于GROUP BY子句,在SQL Server 2008提供的所有聚合函数中,除了COUNT函数以外,聚合函数都会忽略空值AVG.COUNT.COUNT_BIG ...

随机推荐

  1. spring boot项目中无法访问resources文件夹问题

    如图,浏览器默认访问static文件下的内容,无法访问templates文件下的html文件. 解决方法: 在application.properties文件中添加静态资源目录的配置即可.

  2. vue路由元之进入路由需要用户登录权限功能

    为什么需要路由元呢??? 博猪最近开发刚刚好遇到一个情况,就是有个路由页面里面包含了客户的信息,客户想进这个路由页面的话, 就可以通过请求数据获取该信息,但是如果客户没有登录的话,是不能进到该页面的, ...

  3. C#winform跨窗体传值和调用事件的办法

    有三个窗体,分别是Main主窗体,Form1窗体1,From2窗体2,其中Main是主窗体,Form1窗体1是一个消息通知窗体,Form2窗体2主窗体的一个子窗体,程序启动时,消息框窗体1弹出,通过消 ...

  4. ReactNative报错:Can't find variable: __fbBatchedBridge

    最近开始研究ReactNative,首先根据网上教程 http://www.codeceo.com/article/windows-react-native-android.html 一步一步来.完成 ...

  5. Gradle 多环境URL请求设置

    在开发过程中,多环境配置是经常遇到的,比如在Android开发过程中,在不同环境上请求服务器的URL是不同的,使用Gradle进行管理,是非常方便的. 首先查看工程目录结构: 使用AndroidStu ...

  6. DIV+CSS布局的优势和弊端

    DIV+CSS的优势1.符合W3C标准.这保证您的网站不会因为将来网络应用的升级而被淘汰.2.对浏览者和浏览器更具亲和力.由于CSS富含丰富的样式,使页面更加灵活性,它可以根据不同的浏览器,而达到显示 ...

  7. 小程序使用模板template

    小程序使用模板template 1.介绍:模板就是代码的高度复用,将在很多页面使用了相同的部分可以使用模板封装 <!-- 在页面组件中使用 --> <!-- 此时定义了一个模板 -- ...

  8. vue中使用mixins

    Mixins (混合或混入)——定义的是一个对象 1.概念:一种分发Vue组件可复用功能的非常灵活的方式.混入对象可以包含任意组件选项(组件选项:data.watch.computed.methods ...

  9. DPDK Mbuf Library(学习笔记)

    1 Mbuf库 Mbuf库提供了分配和释放缓冲区(mbufs)的功能,DPDK应用程序可以使用这些mbufs来存储消息缓冲. 消息缓冲存储在内存池中,使用Mempool库. 数据结构rte_mbuf通 ...

  10. 万字长文!一次性弄懂 Nginx 处理 HTTP 请求的 11 个阶段

    Nginx 处理一个 HTTP 请求的全过程 前面给大家讲了 Nginx 是如何处理 HTTP请求头部的,接下来就到了真正处理 HTTP 请求的阶段了.先看下面这张图,这张图是 Nginx 处理 HT ...