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.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
随机推荐
- (cvpr2019) The Degradation Model and Solution of DPSR
新的退化模型: $y = (x\downarrow_{s}) \otimes k + n $ 其中$\downarrow_{s}$代表尺度因子为$s$的双三次下采样,$y$表达的是低分辨率图像(经过双 ...
- 重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’
view中导入:from django.contrib.auth.models import AbstractBaseUser settings.py中设置了:AUTH_USER_MODEL='app ...
- Pandas:深市股票代码前补足0
#深市代码前补充0----------------- df[' #先增加一列 #将2列合并为新列 df['代码合并'] = df['补充'] + df['股票代码'] #再取后6位 df['股票代码' ...
- Java第一次实训课的作业
1.圆的面积 2.加密数字 3.奇偶数
- CentOS下使用tcpdump网络抓包
tcpdump是Linux下的截获分析网络数据包的工具,对优化系统性能有很大参考价值. 安装 tcpdump不是默认安装的,在CentOS下安装: yum install tcpdump 在Ubunt ...
- Android中本地广播的实现
其实Android的本地广播并没有什么好讲的,他就是用了一个localbroadcastmanager类来sendbroadcast,以及注册和注销广播,没有什么特点,其中实例该类的时候用了getin ...
- 【POJ 1179】Polygon
[原题链接]传送门 [题解思路] 1.第一感觉没有其他做法,想到动态规划,去环,区间dp 2.f[l,r]表示[l,r]内的最大值,考虑转移 3.最大值分加法和乘法,其中乘法不一定由两个要求合并的区间 ...
- 3、Docker镜像管理基础
Docker image # docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE redis -alpine 23d561d12e92 d ...
- 线程(Thread,ThreadPool)、Task、Parallel
线程(Thread.ThreadPool) 线程的定义我想大家都有所了解,这里我就不再复述了.我这里主要介绍.NET Framework中的线程(Thread.ThreadPool). .NET Fr ...
- syncbackse操作
有4类操作,包括同步,备份,镜像和组 同步是前文件夹内和后文件夹内的文件同步,使得二者的所有文件保持一致,但是也可以修改不同类型文件的覆盖操作,如果左侧有文件123.txt,右侧无123.txt文件, ...