Hive SQL 练习
来源:互联网 发布:鹰眼行车记录仪软件 编辑:程序博客网 时间:2024/05/22 21:53
点击有惊喜
首先将练习用的两张表 上传到hdfs上
命令如下:
hadoop fs -put /opt/dep.txt /dep.txt
hadoop fs -put /opt/employee.txt /employee.txt
然后开始写练习
创建数据库
create database bs17;
use bs17;
创建员工信息表
create table employee(
emp_id string
,emp_name string
,status string
,salary string
,status_salary string
,in_work_date string
,leader_id string
,dep_id string
)row format delimited
fields terminated by '\t'
row format delimited fileds terminated by '\t' 表示将传进来的文件按照空格分割 写入到表格中
闯入文件 employee.txt
load data inpath '/employee.txt' overwrite into table employee;
查询表
select * from employee;
同理创建第二个表
create table department(
dep_id string
,dep_name string
,address string
)row format delimited
fields terminated by '\t'
传入文件 以空格分割写入到表格中
load data inpath '/dep.txt'overwrite into table department;
查询表
select * from department
之前的创建表 都是采用string字段 这样做的原因是防止传入的数据损失精度失真 以原文件的格式类型为准 txt 通用 string
接下来通过 as 来更改表结构 将字段改为所需要的
create table dw_employee as select
cast(emp_id as int) emp_id
,emp_name
,status
,cast(salary as double)salary
,cast(status_salary as double)status_salary
,cast(in_work_date as date)in_work_date
,cast(leader_id as int)leader_id
,cast(dep_id as int)dep_id
from employee
dw_employee 为创建好的更改好字段的表
select * from dw_employee
查询表结构语句
describe formatted dw_employee
同理更改第二张表的表结构
create table dw_department as
select cast(dep_id as int)dep_id
,dep_name
,address
from department
select * from dw_department
除了 通过 as 复制表 之外 还可以通过 like 来克隆 不过 like 克隆的只有表结构 没有数据
create table employee_clone like employee;
select * from employee_clone
查找employee 表中的 id name 月薪 季薪 年薪
select emp_id
,emp_name
,salary mon_salary
,salary*3 season_salary
,salary*12 year_salary
from dw_employee
查找员工的月工资 月薪加奖金
select emp_id
,emp_name
,salary+status_salary getMoney
from dw_employee
插入数据 id name 其他为null
insert into dw_employee(emp_id,emp_name) values(1111,'aaaa')
nvl 表示条件 如果有字段信息 则表示字段信息 如果为null 则用后的表示
如有职位 则表示 没有则用普通员工代替
如果有入职日期 则表示 没有 则用2015-5-1表示
select emp_id
,emp_name
,nvl(status,'普通员工') job
,nvl(in_work_date,'2015-5-1') in_work_date
from dw_employee
复制员工表,表名为emp_copy
create table emp_copy as select * from dw_employee
机构中有多少种职位(就是将所有的status 聚合展示出来 展示出来几种就有几种)
select distinct status from dw_employee
薪水高于6000的员工
select * from dw_employee where salary>6000
职位是analyst 的员工
select * from dw_employee where status ='analyst'
以小写格式展示职位信息(lower())
select emp_id
,emp_name
,lower(status)
from dw_employee
忽略大小写匹配职位等于‘ANALYST’的记录
select * from dw_employee where upper(status)='ANALYST'
查询出薪水在5000到8000之间的员工(between and)
select * from dw_employee where salary between 5000 and 8000;
查询出2016年入职的员工
select * from dw_employee where year(in_work_date)=2016
薪水不在5000到8000的员工
select * from dw_employee where salary not between 5000 and 8000;
查询出职位是Manager或者analyst的员工
select * from dw_employee where status in(Manager,analyst);
模糊查询like %
查询出没有岗位工资的员工
select * from dw_employee where status_salary is null;
查询有岗位工资的员工
select * from dw_employee where status_salary is not null;
查询出不在10部门和不再30部门的员工
select * from where dw_employee where dep_id not in(10,30)
点击有惊喜
- Hive SQL 练习
- hive练习
- hive sql
- HIVE SQL
- hive sql
- 关于Hive的练习
- hive练习数据
- Hive安装配置+练习
- Hive HQL练习
- sql练习
- SQL练习
- sql练习
- sql练习
- sql练习
- sql 练习
- SQL练习
- sql练习
- sql练习
- 2017-10-26
- plsql解决中文乱码问题
- 直观地打印二叉树
- sh脚本编程简介
- C++构造与析构(18)
- Hive SQL 练习
- C#路径中获取文件全路径、目录、扩展名、文件名称
- 解决python27 matplotlib 中文不显示问题
- 支付宝app支付java服务端
- Promise封装ajax请求
- 3个基于Java的Blockchain库
- Eclipse快捷键收集
- Nginx https反向代理和负载均衡服务器配置(centos)
- 工作日记2017.10.23 Mac 环境变量的位置、查看和添加PATH环境变量