案例一
练习:
一:将下列数据加载hive表。
员工信息表emp:
字段:员工id,员工名字,工作岗位,部门经理,受雇日期,薪水,奖金,部门编号
英文名:EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,BONUS,DEPTNO
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 ',';
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
1600 1250 2850 1500 950
部门信息表dept:
字段:部门编号,部门名称,部门地点
英文名:DEPTNO,DEPTNAME,DEPTADDR
create table dept(
DEPTNO int
,DEPTNAME string
,DEPTADDR string
)
row format delimited
fields terminated by ',';
10,ACCOUNTING,NEW YORK
10,ACCOUNTING,shanghai
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
二:使用HQL完成下面需求:
1. 列出至少有一个员工的所有部门。
select t1.deptno,t1.dname,t1.loc from dept t1 join (select deptno,count(distinct deptno) as cnt from emp group by deptno) t2 on (t1.deptno=t2.deptno);
2. 列出薪金比“SMITH”多的所有员工。薪资+津贴 12*sal+COMM
在Hive中,where条件后面不支持不等式的子查询,可以使用join的方式实现
select t1.empno,t1.ename,t1.sal_comm from (select empno,ename,12*sal+nvl(comm,0) as sal_comm,1 as temp_id from emp) t1 join (select 12*sal+nvl(comm,0) as sal_comm,1 as temp_id from emp where ename='SMITH') t2 on (t1.temp_id=t2.temp_id) where t1.sal_comm>t2.sal_comm;
美化后的sql:
SELECT t1.empno, t1.ename, t1.sal_comm
FROM (
SELECT empno, ename
, 12 * sal + nvl(comm, 0) AS sal_comm
, 1 AS temp_id
FROM emp
) t1
JOIN (
SELECT 12 * sal + nvl(comm, 0) AS sal_comm
, 1 AS temp_id
FROM emp
WHERE ename = 'SMITH'
) t2
ON t1.temp_id = t2.temp_id
WHERE t1.sal_comm > t2.sal_comm;
3. 列出所有员工的姓名及其直接上级的姓名。
select t1.ename,t2.ename as leader_name from emp t1 join emp t2 on (t1.mgr=t2.empno);
4. 列出受雇日期早于其直接上级的所有员工。
select t1.ename,t1.hiredate,t2.ename as leader_name,t2.hiredate as leader_hiredate from emp t1 left join emp t2 on (t1.mgr=t2.empno) where t1.hiredate<t2.hiredate;
5. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
select * from dept t1 left join emp t2 on (t1.deptno=t2.deptno);
6. 列出所有“CLERK”(办事员)的姓名及其部门名称。
select * from (select * from emp where job='CLERK') t1 join dept t2 on (t1.deptno=t2.deptno);
7. 列出最低薪资大于1500的各种工作。
select job,min(sal) as min_sal from emp group by job having min_sal>1500;
select * from (select job,min(sal) as min_sal from emp group by job) t1 where t1.min_sal>1500;
8. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号
select empno,ename from emp where deptno in (select deptno from dept where dname='SALES');
9. 列出薪金高于公司平均薪金的所有员工。 12*sal+comm
select t1.empno,t1.ename,t1.sum_sal_comm,t2.avg_sal_comm from (select empno,ename,12*sal+nvl(comm,0) as sum_sal_comm,1 as temp_id from emp) t1 join (select round(avg(12*sal+nvl(comm,0)),2) as avg_sal_comm,1 as temp_id from emp) t2 on(t1.temp_id=t2.temp_id) where t1.sum_sal_comm>t2.avg_sal_comm;
10.列出与“SCOTT”从事相同工作的所有员工。
select empno,t1.ename,job from emp t1 where job in (select job from emp where ename='SCOTT') and t1.ename!='SCOTT';
11.列出薪资等于部门30中员工的薪资的所有员工的姓名和薪资。
select t1.ename,t1.sal from emp t1 where t1.sal in (select sal from emp where deptno=30) and t1.deptno!=30;
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select max(12*sal+nvl(comm,0)) from emp where deptno=30;
select 12*sal+nvl(comm,0) from emp where deptno!=30;
select t1.ename,t1.sum_sal_comm,t2.max_sal_comm from (select ename,12*sal+nvl(comm,0) as sum_sal_comm,1 as temp_id from emp where deptno!=30) t1 join (select max(12*sal+nvl(comm,0)) as max_sal_comm,1 as temp_id from emp where deptno=30) t2 on (t1.temp_id=t2.temp_id) where t1.sum_sal_comm>t2.max_sal_comm;
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select deptno,count(empno) as nop,round(avg(12*sal+nvl(comm,0)),2) as avg_sal_comm,floor(avg(datediff(current_date(),hiredate))) as avg_work_days from emp group by deptno;
14.列出所有员工的姓名、部门名称和工资。
select t1.ename,t2.dname,t1.sal from emp t1 join dept t2 on (t1.deptno=t2.deptno);
15.列出所有部门的详细信息和部门人数。
select t2.deptno,t2.dname,t2.loc,if(t1.pcount is null,0,t1.pcount) from (select deptno,count(distinct empno) as pcount from emp group by deptno) t1 right join dept t2 on (t1.deptno=t2.deptno);
select t2.deptno,t2.dname,t2.loc,nvl(t1.pcount,0) from (select deptno,count(distinct empno) as pcount from emp group by deptno) t1 right join dept t2 on (t1.deptno=t2.deptno);
16.列出各种工作的最低工资。
select min(sal) from emp group by job;
17.列出各个部门的MANAGER(经理)的最低薪金。
select t1.deptno,min(12*sal+nvl(comm,0)) from (select deptno,sal,comm from emp where job='MANAGER') t1 group by t1.deptno;
18.列出所有员工的年工资,按年薪从低到高排序。
select empno,ename,12*sal+nvl(comm,0) as year_sal from emp order by year_sal;
19. 列出每个部门薪水前两名最高的人员名称以及薪水。(思考:用开窗函数解决)
select ename,deptno,sal,rows_number() over(partition by denptno order by sal desc) from emp;
select w.empno,w.ename,w.sal,w.deptno,w.rn from (select empno,ename,sal,deptno,row_number() over(partition by deptno order by sal desc) as rn from emp) w where w.rn<3;
20. 列出每个员工从受雇开始到2018-12-12 为止共受雇了多少天。
select empno,ename,datediff('2018-12-12',hiredate) as days from emp;
案例二
sql:Hive实现按照指定格式输出每七天的消费平均数
输出格式:
2018-06-01~2018-06-07 12.29
...
2018-08-10~2018-08-16 80.67
数据如下:
2018/6/1,10
2018/6/2,11
2018/6/3,11
2018/6/4,12
2018/6/5,14
2018/6/6,15
2018/6/7,13
2018/6/8,37
2018/6/9,18
2018/6/10,19
2018/6/11,10
2018/6/12,11
2018/6/13,11
2018/6/14,12
2018/6/15,14
2018/6/16,15
2018/6/17,13
2018/6/18,17
2018/6/19,18
2018/6/20,19
2018/6/21,20
2018/6/22,21
2018/6/23,21
2018/6/24,22
2018/6/25,24
2018/6/26,25
2018/6/27,23
2018/6/28,27
2018/6/29,28
2018/6/30,29
2018/7/1,40
2018/7/2,41
2018/7/3,41
2018/7/4,42
2018/7/5,44
2018/7/6,45
2018/7/7,43
2018/7/8,47
2018/7/9,48
2018/7/10,49
2018/7/11,50
2018/7/12,51
2018/7/13,51
2018/7/14,52
2018/7/15,54
2018/7/16,55
2018/7/17,53
2018/7/18,57
2018/7/19,58
2018/7/20,59
2018/7/21,30
2018/7/22,31
2018/7/23,31
2018/7/24,32
2018/7/25,34
2018/7/26,35
2018/7/27,33
2018/7/28,37
2018/7/29,38
2018/7/30,39
2018/7/31,70
2018/8/1,71
2018/8/2,71
2018/8/3,72
2018/8/4,74
2018/8/5,75
2018/8/6,73
2018/8/7,77
2018/8/8,78
2018/8/9,79
2018/8/10,80
2018/8/11,81
2018/8/12,81
建表语句:
create table f
(
date_time string,
cost string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
答案:
1、先将日期进行切分得到年月日
select split(date_time,'/'),cost from f;
2、将切分出来的年月日使用'-'进行连接,使用concat_ws
select concat_ws('-',split(date_time,'/')),cost from f;
3、主要实现的思路是将每7天分成一组,问题就是如何实现统一分组,首先我可以将每个日期对6月1号做减法,得到间隔的日期
datediff
select datediff(concat_ws('-',split(date_time,'/')),'2018-6-1'),cost from f;
4、在上面得到间隔天数之后,进行除以7,floor向下取整,得到整数部分
select floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7),cost from f;
5、现在需要思考的问题是,怎么转化成最终结果的日期格式?而且还必须是7天为一组的现象,
select date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),cost from f;
6、现在需要考虑的问题是如何获取的7天后的日期,以及输出的格式怎么定义,通过观察上一步的结果发现,在当前的日期基础上加上6就是一周的结束时间
,然后使用字符串拼接函数进行拼接得到最终结果
select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~',date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f;
7、分组求平均值
select w.dtime,avg(w.cost) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~',date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f) w group by w.dtime;
8、四舍五入,保留两位小数
select w.dtime,round(avg(w.cost),2) from (select concat(date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7 as int)),'~',date_add('2018-6-1',cast(floor(datediff(concat_ws('-',split(date_time,'/')),'2018-6-1')/7)*7+6 as int))) as dtime,cost from f) w group by w.dtime;