Hive(一)基础补充

数值类型可以进行加减乘除
select rank+1 from sogou limit 10;
给rank平方
pow()
select pow(rank,2) from sogou limit 10;
模(取余)
pmod()
select pmod(rank,2) from sogou limit 10;
强制类型转换
cast() 注意:cast()只是改变了查询时的数据类型,原表中列的数据类型并没有改变。
select cast(time as bigint) from sogou limit 10;
select cast(rank as string) from sogou limit 10;


将String类型的值链接起来 concat()
select concat(uid,keyword) from sogou limit 10;
字符串的长度 length()
select keyword,length(keyword) from sogou limit 10;
表的嵌套
select
sum(if(s.cnt=1,1,0)),
sum(if(s.cnt=2,1,0)),
sum(if(s.cnt=3,1,0)),
sum(if(s.cnt>3,1,0)) from 
(select uid,count(*) as cnt from sogou group by uid) s;
统计rank <=3 && order >1的用户个数:
select count(distinct s.uid) from 
(select * from sogou where rank<=3 and order >1) s;


where过滤
like的使用
查询url中以http开头进行查询的数量
select count(*) from sogou where url like 'http%' limit 10;


group by 分组查询
(一个字段)
select rank,count(*) as cnt from sogou group by rank limit 50;
(多个字段)
select rank,order,count(*) as cnt from sogou
group by rank,order;


having 需要在group by的基础上进行操作
select rank,order,count(*) as cnt from sogou
group by rank,order having cnt >3;


排序 order by   asc升序(default)  desc降序
select rank,order,count(*) as cnt from sogou
group by rank,order having cnt >3 order by cnt desc;


UNION ALL 合并数据集
查询sogou与sogou_201608两张表中rank <10 的用户个数


select count(distinct s.uid) from 
(select * from sogou where rank <10 
 union all
select * from sogou_201608 where rank <10) s;


查询sogou与sogou_201608两张表中所有的条数
select count(*) from 
(select * from sogou 
union all 
select * from sogou_201608)s;

你可能感兴趣的:(Hive)