HQL示例
创建并导入表单
1 | hive> create table info(id int,name string,sal int) row format delimited fields terminated by '\t' lines terminated by '\n'; |
查询(select)
- 查询条目数:
select count(*) from info;
- 获取总薪水:
select sum(sal) from info;
- 获取薪水最高的3个ID与对应薪水:’select id,sal from info order by sal desc limit 3;’
- 获取大部分人的薪水状况:
select sal,count(*) as sum from info group by sal order by sum desc;
————以薪水为组别按照出现次数降序排序 - 以2000为区间统计薪水分布区间:
select concat(floor(sal/2000)*2000,'-',(floor(sal/2000)+1)*2000),count(*) from info group by floor(sal/2000);
————对sal除以2000向下取整作为分组依据,可以取得一定范围内的数值,例如floor(3000/2000)=1;floor(3600/2000)=1。 - 获取薪水大于8000的员工的姓名:
hive> select name,sal from info where sal>8000;
- 获取每个ID的总工资:
select id,sum(sal) from info group by id;
- 获取每个ID的平均工资并按照工资降序排列:
select id,avg(sal) as avg_sal from info group by id order by avg_sal desc;
表单导出
1 | insert overwrite local directory '/root/' row format delimited fields terminated by '\t' select count(*) from loan |
其他
- 区间分析:
1
2
3
4
5
6
7select count(case when length(text) between 0 and 20 then 1 end) as text1,
count(case when length(text) between 21 and 40 then 1 end) as text2,
count(case when length(text) between 41 and 60 then 1 end) as text3,
count(case when length(text) between 61 and 80 then 1 end) as text4,
count(case when length(text) between 81 and 100 then 1 end) as text5,
count(case when length(text) > 100 then 1 end) as text6
from table;
参考文档
本文标题:HQL示例
文章作者:Raincorn
发布时间:2020-07-22
最后更新:2020-07-28
原始链接:https://blog.raincorn.top/2020/07/22/HQL_Examples/
版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可