join,innerjoin,leftjoin,rightjoin,fulljoin,union,left semi joinjoin--关联查询 如下select*from
a
join
b
join
c
on a.id=b.id and b.id=c.id
leftjoin--左关联 rightjoin--又关联
innerjoin -- 内联 如下 跟直接join的结果一样 都会产生笛卡尔积select*from a
innerjoin b
left semi join--类似于子查询 把左表当主表 返回关联条件相同的左表数据select--注意只返回左表数据 条件是右表也有的关联数据的*from
tb_b
left semi join
tb_a
on tb_a.id = tb_b.id ;union--连接两个查询的结果集 要求字段个数和数据类型一致 union all不会去重 union去重select--注意必须两个表数据列一致 数据类型一致才可关联*from
tb_a
unionallselect*from
tb_a ;fulljoin-- 关联俩表 没有的数据用null补齐select*from a
fulljoin b
on a.id = b.id ;
排序
在执行SQL的时候默认是一个reducetesk
set mapreduce.job.reduces=n;-- 配置reduce的个数 set mapreduce.job.reduces;-- 查看配置结果
orderby 全局最终结果排序
distribute by 指定分区字段 分区
sort by-- 区内数据排序
cluster by 当分区字段和排序字段相同 并且是升序的时候使用cluster by 替换 distribute by sort by
orderby--全局最终结果排序select*from tb_a orderby id --不写默认升序(asc)select*from tb_a orderby id desc--倒序---------------------------------------------------------------------------------------
distribute by--指定分区字段 分区select*from tb_x distribute by name;--指定分区字段 分区--------------------------------------------------------------------------------------
sort by--区内数据排序select*from tb_x distribute by name sort by name desc;--和distribute by联合使用的时候sort在后---------------------------------------------------------------------------------------
cluster by--当分区字段和排序字段相同 并且是升序的时候使用cluster by 替换 distribute by sort byselect*from tb_x distribute by name sort by name desc;select*from tb_x cluster by name ;--使用cluster代替distrubute和sort,只能升序使用
王奔 A 男
娜娜 A 男
宋宋 B 男
凤姐 A 女
热巴 B 女
慧慧 B 女
createtable tb_emp(
name string ,
dname string ,
gender string
)row format delimited fieldsterminatedby"\t";loaddatalocal inpath "/data/12.2/"intotable tb_emp ;
根据分组AB分组然后求出男女各多少
男 女
A 21
B 12
select
dname ,sum(case gender when'男'then1else0end) f ,--分组中如果是男就返回1,再sun聚合相加sum(case gender when'女'then1else0end) m
from
tb_emp
groupby
dname ;