hive练习题
记录一下做的hive练习题,有需要请自行练习,附答案。
数据
1.员工信息表emp:
字段:员工id,员工名字,工作岗位,部门经理,受雇日期,薪水,奖金,部门编号
英文名:EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,BONUS,DEPTNO
7369,SMITH,CLERK,7902,1980-12-17,800,null,20
7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-22,1250,500,30
7566,JONES,MANAGER,7839,1981-04-02,2975,null,20,
7654,MARTIN,SALESMAN,7698,1981-09-28,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01,2850,null,30
7782,CLARK,MANAGER,7839,1981-06-09,2450,null,10
7788,SCOTT,ANALYST,7566,1987-04-19,3000,null,20
7839,KING,PRESIDENT,null,1981-11-17,5000,null,10
7844,TURNER,SALESMAN,7698,1981-09-08,1500,0,30
7876,ADAMS,CLERK,7788,1987-05-23,1100,null,20
7900,JAMES,CLERK,7698,1981-12-03,950,null,30
7902,FORD,ANALYST,7566,1981-12-03,3000,null,20
7934,MILLER,CLERK,7782,1982-01-23,1300,null,10
2.部门信息表dept:
字段:部门编号,部门名称,部门地点
英文名:DEPTNO,DEPTNAME,DEPTADDR
10,ACCOUNTING,NEW YORK
10,ACCOUNTING,shanghai
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
题目
1.列出至少有一个员工的所有部门。
2. 列出受雇日期早于其直接上级的所有员工。
3. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
4. 列出最低薪金大于1500的各种工作。
5. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号
6. 列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
7. 列出在每个部门工作的员工数量、平均工资和平均服务期限。
8. 列出所有部门的详细信息和部门人数。
9. 列出各种工作的最低工资
10.列出所有员工的年工资,按年薪从低到高排序。
11.列出每个部门薪水前两名最高的人员名称以及薪水。
12.列出每个员工从受雇开始到2018-12-12 为止共受雇了多少天。
实现代码
1.建表
#创建表emp
create table emp
(empno int , ename string , job string , mgr int , hiredate string , sal int , bonus int , deptno int )
row format delimited fields terminated by ",";
#导入数据
load data local inpath "/soft/emp.txt" into table emp ;
#创建表dept
create table dept
(deptno int , deptname string , deptaddr string )
row format delimited fields terminated by ",";
#导入数据
load data local inpath "/soft/dept.txt" into table dept ;
2.题解
select deptname from dept join emp on dept.deptno = emp.deptno group by dept.deptname having count(*) >=1 ;
-- 条件编写 emp1员工表 emp2领导表
select emp1.* from emp as emp1 join emp as emp2 on emp1.mgr = emp2.empno
where emp1.hiredate < emp2.hiredate ;
-- 展示的主表 dept 部门表
select * from dept left join emp on dept.deptno =emp.deptno ;
-- 薪金表达 薪水+奖金 sal+bonus 如果bonus是null就显示0,否则就显示bonus nvl(bonus , 0)
select job from emp group by job having min(sal+nvl(bonus,0)) > 1500 ;
select ename from emp where deptno in
(select deptno from dept where deptname = "SALES");
-- 查出在部门30工作的员工最高工资
select max(sal+nvl(bonus,0)) from emp where deptno =30 ;
-- 查出员工工资高于上述的信息( hive 不支持 > 的 where子查询 )
从 select ename , sal+nvl(bonus , 0)
from (select * , 1 as tmp from emp ) emp1
join (select max(sal+nvl(bonus,0)) as maxsal , 1 as tmp from emp where deptno =30 ) emp2
on emp1.tmp = emp2.tmp
and emp1.deptno <> 30
and (sal+nvl(bonus,0))>maxsal ;
服务期限:工作时间 今天current_date()-入职时间hiredate datediff()
select deptno , count(ename) , round(avg(sal),2), round(avg(datediff(current_date(),hiredate))) from emp group by deptno ;
select deptno , deptname , deptaddr ,count(*)from dept join emp on dept.deptno = emp.deptno group by deptno ;
-- 查询每个部门的人数
select deptno ,count(*) from emp group by deptno ;
-- 查询每个部门信息 左连接 每个部门的人数
select dept.*, total from dept left join (select deptno ,count(*) as total from emp group by deptno) emp1 on dept.deptno =emp1.deptno ;
select job ,min(sal) from emp group by job ;
-- 年薪 sal*12+bonus
select ename , (sal*12+nvl(bonus,0)) as year_sal from emp order by year_sal ;
-- 先将部门的排序搞出来
select ename ,sal ,dense_rank() over(partition by deptno order by sal desc) as rn ,deptno from emp;
-- 把上面的查询结果变成表,就可以直接加条件
select ename , sal , deptno from (select ename ,sal ,row_number() over(partition by deptno order by sal desc) as rn ,deptno from emp) emp1 where rn<=2 ;
select ename , datediff('2018-12-12',hiredate) from emp ;