当前位置: 首页 > news >正文

Hive SQL 练习(这个秒退是怎么回事啊?写了半天 东西都没了,瞬间整个人都凌乱了)...

首先将练习用的两张表 上传到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)

DDL 数据定义语言
insert select

create table employee_leader like dw_employee
insert into dw_employee_leader select * from dw_employee where leader_id is null

insert overwrite table dw_employee_leader select * from dw_employee where leader_id is null

排序问题
oder by 升序 asc 降序 desc
select * from dw_employee order by salary desc;

hive 的order by 全排序是通过只设置一个reducer 节点的方式来实现的
通过hive可以很轻松的实现二次排序
select * from dw_employee order by salary desc,salary_status desc;

查询多少个员工
select count(*) from dw_employee

分组表达式 Group by 聚合函数 Count max min avg sum
查询有多少个员工
select count(*) from dw_employee
查询有多少个姓张的员工
select count(*) from dw_employee where emp_name like '%张%'
计算员工的总薪水是多少
select sum(salary) from dw_employee

计算员工的人数总和、薪水综合、平均薪水,最高薪水、最低薪水
select

count(*)
,sum(salary)
,avg(salary)
,max(salary)
,min(salary)

from dw_employee

求出每个部门的最高薪水、最低薪水、平均薪水、总薪水、总人数
select
count(*)

,sum(salary)
,avg(salary)
,max(salary)
,min(salary)

from dw_employee
group by dep_id;

----求出总人数超过两人的部门的最高 最低 平均 总薪水

select
count(*)

,sum(salary)
,avg(salary)
,max(salary)
,min(salary)

from dw_employee
group by dep_id;
having count(*)>2

having 的作用 是在聚合后做一次删选 Having 用来对分组后的结果进行进一步的过滤

select * from dw_employee where salary in (select max(salary) from dw_employee)

-----in 在后面添加子查询结果作为判断条件 如果子查询中有索引的话 in 是用不到这个索引 因此 in对接子查询的效率最低

子查询
查询出最高薪水的人的信息
select * from dw_employee where salary in (select max(salary) from dw_employe)
最低薪水的人
select * from dw_employee where salary in (select min(salary) from dw_employe)

工资高于平均薪水的人的信息
目前 hive目前不支持子查询的非等值查询
所以使用 笛卡尔 乘积的方式

因为参数原因 不允许使用笛卡尔 乘积 所以需要通过指令设置一下让它允许
set hive.strict.checks.cartesian.product=false
select a.*

,b.*

from dw_employee a, (select avg(salary)avesalary from dw_employee)b
where a.salary >b.avgsalary
谁的薪水比小赵的薪水高
select a.*
from dw_employee a,(select salary from dw_employee where emp_name='小赵')b

where a.salary>b.salary
研发部有哪些职位
select status

from dw_employee a

where exists(select dep_id

    from  department b
     where dep_name ='研发部' 
    and a.dep_id =b.dep_id)

ALL 子查询 Any(子查询)

谁是妖姬的同部门的同事
select * from dw_employee where dep_id in(select dep_id from dw_employee where emp_name ='妖姬')

如果有两个叫妖姬 谁是妖姬的同事???

inner join 是两张表根据关联条件 相互过滤 能够关联上的数据才会出现在结果集中

Exists
not exists
union 去重
union all 不去重

薪水大于8000或者小于2000或者等于5000的员工
select * from dw_employee where salary>8000
union
select * from dw_employee where salary <2000
union
select * from dw_employee where salary=5000

交集 intersect
差集 minus

lefter outer join right outer join

相关文章:

  • SylixOS之TFTP使用
  • mysql初探
  • 洛谷——P2862 [USACO06JAN]把牛Corral the Cows
  • web开发经验
  • Zookeeper+ActiveMQ 集群实现
  • Android 使用DDMS查看内存使用情况
  • 新品牌如何开展网络营销?
  • 什么是自动化运维 ? 自动化运维的设计思路以及实战
  • 1.3给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。...
  • html+css+JavaScript例题
  • 通过递归的方式将字符串逆置打印
  • Oracle osw监控工具的使用示例
  • ASP.NET 跨平台应用开发
  • linux负载查看
  • 【漫谈数据仓库】 如何优雅地设计数据分层
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • Facebook AccountKit 接入的坑点
  • Git 使用集
  • leetcode讲解--894. All Possible Full Binary Trees
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • PV统计优化设计
  • Spring Boot快速入门(一):Hello Spring Boot
  • SSH 免密登录
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 官方解决所有 npm 全局安装权限问题
  • 人脸识别最新开发经验demo
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 源码安装memcached和php memcache扩展
  • 正则学习笔记
  • 做一名精致的JavaScripter 01:JavaScript简介
  • MyCAT水平分库
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • $.proxy和$.extend
  • (0)Nginx 功能特性
  • (C语言)fgets与fputs函数详解
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (四)c52学习之旅-流水LED灯
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)Sublime Text3配置Lua运行环境
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • .Net Web窗口页属性
  • .NET 使用配置文件
  • .Net 知识杂记
  • .NET处理HTTP请求
  • .NET面试题(二)
  • .NET企业级应用架构设计系列之开场白
  • /boot 内存空间不够
  • @html.ActionLink的几种参数格式
  • @Transactional 竟也能解决分布式事务?
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [Android]常见的数据传递方式