Hive 外部表的练习(多表关联查询,以及分组,子查询)
Hive 外部表的练习
hive创建库和表操作
hive加载数据,4种发放
1.数据放在Linux创建表结构 进行加载
2.先定义表结构、将一个查询select语句结果插入到表中
3.数据先放在hdfs \ 创建表结构\ 进行加载(hive 只能加载目标文件的上级目录)
4.外部数据 external 内部表和外部表 使用上没有任何区别,删除时则有差别
数据:
创建表,以及添加数据:
create external table if not exists my_course(
courseid string,
couresename string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_course';
create external table if not exists my_source(
userid string
courseid string,
score string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_sourcet'
create external table if not exists my_student(
userid string
name string
sex string
age string,
xi string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_student'
1.问题:查询全体学生的学号与姓名
select
userid,name
from my_student;
2.问题:查询选修了课程的学生姓名和课程名称
select student.name,t.couresename
from(
select course.couresename couresename,score.userid userid
from my_score score,my_course course
where score.courseid=course.courseid) t,my_student student
where t.userid=student.userid;
3.问题:查询每个选修的课程共选了多少人
select t.couresename,count(*) num
from(
select course.couresename couresename,score.userid userid
from my_score score,my_course course
where score.courseid=course.courseid) t,my_student student
where t.userid=student.userid
group by t.couresename
order by num desc
limit 3;
4.问题:查询学生的总人数
select count(distinct(userid))
from my_student;
5.问题:计算数据库课程的学生平均成绩
select course.couresename,avg(score.score)
from my_score score,my_course course
where course.couresename='数据库' and score.courseid=course.courseid
group by course.couresename;
6.问题:查询选修数学课程的学生最高分数
select max(score.score)
from my_score score,my_course course
where course.couresename='数学' and score.courseid=course.courseid;
7.问题:查询选修了3门以上的课程的学生姓名
select student.name,t.num
from(
select userid,count(*) num
from my_score
group by userid ) t,my_student student
where t.userid =student.userid and t.num>=3;
8.问题:按照年龄排序并直接输出到不同的文件中
create table if not exists result2(
userid string,
name string,
sex string,
age string,
xi string
)
row format delimited fields terminated by ','
stored as textfile;
insert into result2
select *
from my_student
order by age desc;
9.问题:查询学生的得分情况。
select student.name,score.score
from my_score score,my_student student
where score.userid=student.userid;
10.问题:查询选修信息系统课程且成绩在90分以上的所有学生。
select distinct(t2.name)
from(
select score.userid userid
from my_score score,my_course course
where score.score>=90 and score.courseid=course.courseid) t1,my_student t2
where t1.userid=t2.userid;
11.问题:查询与“刘晨”在同一个系学习的其他学生
select t3.name
from(
select t2.name name
from my_student t1,my_student t2
where t1.name='刘晨' and t1.xi=t2.xi) t3
where t3.name<>'刘晨';
Hive 外部表的练习(多表关联查询,以及分组,子查询)的更多相关文章
- 黑马MySQL数据库学习day03 级联 多表查询 连接和子查询 表约束
/* 存在外键的表 删表限制: 1.先删除从表,再删除主表.(不能直接删除主表,主表被从表引用,尽管实际可能还没有记录引用) 建表限制: 1.必须先建主表,再建从表(没有主表,从表无法建立外键关系) ...
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
- Mysql基础语法-建库-建表(增、删、改、查、表关联及子查询)
前言:MySQL是一个数据库管理系统,也是一个关系数据库.它是由Oracle支持的开源软件,MySQL可以在各种平台上运行UNIX,Linux,Windows等.可以将其安装在服务器甚至桌面系统上. ...
- oracle多表关联查询和子查询
oracle多表关联查询和子查询 一.多表关联查询 例子: SQL> create table student1 ( sid ), sname ), sage )); Table created ...
- mysql查询语句 和 多表关联查询 以及 子查询
原文地址: http://blog.csdn.net/github_37767025/article/details/67636061 1.查询一张表: select * from 表名: 2.查询指 ...
- MS sql server 基础知识回顾(二)-表连接和子查询
五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...
- MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- MySQL多表查询之外键、表连接、子查询、索引
MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...
- MySQL数据库学习笔记----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
随机推荐
- 按下回车默认提交form表单问题
最近开发中碰到一个问题,项目中有几个列表展示页面,允许用户通过查询条件模糊查询数据.用户录入关键字后点击回车会调用查询方法根据关键字查询,原先功能没有问题,但是最近发现在查询输入框中按下回车会直接刷新 ...
- Character
Character a = new Character(); Character.isUpperCase(a) 判断给点的字符是否是大写字符 Character.isLowerCase(a) 判断给定 ...
- JS 页面表格的操作
var showObj = null;var arr = [ ['编号','姓名','性别','年龄','备注','操作'], ['1','lisi','nan','12','66666'], ['2 ...
- JS中常见设计模式总结
github: https://github.com/14glwu/FEInterviewBox/tree/master/JS%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F ...
- Oracle基础体系浅析
不论是开发.管理.优化还是设计,对Oracle的基本原理的了解都是必不可少的,于是对自己最近关于Oracle的学习作出一点点的总结. 庖丁解牛之所以能做到"合于桑林之舞,乃中经首之会&quo ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
- spring boot+自定义 AOP 实现全局校验
最近公司重构项目,重构为最热的微服务框架 spring boot, 重构的时候遇到几个可以统一处理的问题,也是项目中经常遇到,列如:统一校验参数,统一捕获异常... 仅凭代码 去控制参数的校验,有时候 ...
- python - Random常用方法记录
import random # range [a,b) 不包含b # 获取随机整数 # randrange [a,b) 不包含b a = random.randrange(0, 101, 5) # E ...
- @resource、@Autowired、@Service在一个接口多个实现类中的应用
Spring在没有引入注解之前,传统的Spring做法是使用.xml文件来对bean进行注入,所有的内容都需要配置在.xml文件中,使配置和编程分离,却增加了可读性和复杂度. Spring注解将复杂的 ...
- H5外包团队 H5开发微信APP的优势有哪些
H5外包团队 H5开发微信APP的优势有哪些