HQL练习
Hive学习笔记总结
05. Hql练习
1. hql基础练习
题目和数据来源:http://www.w2b-c.com/article/150326(去掉-)
create和load
create table students(Sno int,Sname string,Sex string,Sage int,Sdept string)row format delimited fields terminated by ','stored as textfile;
create table course(Cno int,Cname string) row format delimited fields terminated by ',' stored as textfile;
create table sc(Sno int,Cno int,Grade int)row format delimited fields terminated by ',' stored as textfile;
load data local inpath '/home/hadoop/hivedata/students.txt' overwrite into table student;
load data local inpath '/home/hadoop/hivedata/sc.txt' overwrite into table sc;
load data local inpath '/home/hadoop/hivedata/course.txt' overwrite into table course;
1.查询全体学生的学号与姓名
hive> select Sno,Sname from students;
2.查询选修了课程的学生姓名
select distinct Sname from students, sc where students.Sno = sc.Sno;
或:
select distinct Sname from students inner join sc on students.Sno = sc.Sno;
3.查询学生的总人数
select count(*) from students;
4.计算1号课程的学生平均成绩
select avg(Grade) from sc where Cno = 1;
5.查询各科成绩平均分
select Cname,avg(Grade) from sc, course where sc.Cno = course.Cno group by sc.Cno;
//Grade要么出现在group关键词之后,要么使用聚合函数。
6.查询选修1号课程的学生最高分数
select max(Grade) from sc where Cno = 1;
7.求各个课程号及相应的选课人数
select Cno,count(*) from sc group by Cno;
8.查询选修了3门以上的课程的学生学号
select Sno from sc group by Sno having count(Cno) >3 ;
9.查询学生信息,结果按学号全局有序
select * from students order by Sno;
10.查询学生信息,结果区分性别按年龄有序
set mapred.reduce.tasks=2;
select * from students distribute by sex sort by sage;
11.查询每个学生及其选修课程的情况
select students.*,sc.* from students join sc on (students.Sno =sc.Sno);
12.查询学生的得分情况
13.查询选修2号课程且成绩在90分以上的所有学生。
select students.Sname from sc,students where sc.Cno = 2 and sc.Grade > 90 and sc.Sno = students.Sno;
或者:
select students.Sname,sc.Grade from students join sc on students.Sno=sc.Sno where sc.Cno=2 and sc.Grade>90;
14.查询所有学生的信息,如果在成绩表中有成绩,则输出成绩表中的课程号
select students.Sname,sc.Cno from students join sc on students.Sno=sc.Sno;
15.重写以下子查询为LEFT SEMI JOIN
SELECT a.key, a.value FROM a WHERE a.key exist in (SELECT b.key FROM B);
查询目的:查找A中,key值在B中存在的数据。
可以被重写为:
select a.key,a.value from a left semi join b on a.key = b.key;
16.查询与“刘晨”在同一个系学习的学生
select s1.Sname from students s1 where sdept in (select sdept from students where sname = '刘晨');
或者:
select s1.Sname from students s1 left semi join students s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';
注意比较:
select * from students s1 left join students s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';
select * from students s1 right join students s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';
select * from students s1 inner join students s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';
select * from students s1 left semi join students s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';
2. 执行顺序
标准顺序:
select--from--where--group by--having--order by
join操作中,on条件与where条件的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
join发生在where字句之前,在使用left jion时,on和where条件的区别如下:
1、on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。(右边置为Null了)
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
假设有两张表:
表1:tab1
id size
1 10
2 20
3 30
表2:tab2
size name
10 AAA
20 BBB
20 CCC
两条SQL:
1、select * from tab1 left join tab2 on tab1.size = tab2.size where tab2.name='AAA'
2、select * from tab1 left join tab2 on tab1.size = tab2.size and tab2.name='AAA'
第一条SQL的过程:
1、中间表
on条件:
tab1.size = tab2.size
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 20 BBB
2 20 20 CCC
3 30 (null) (null)
2、再对中间表过滤
where 条件:
tab2.name='AAA'
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
第二条SQL的过程:
1、中间表
on条件:
tab1.size = tab2.size and tab2.name='AAA'
(条件不为真也会返回左表中的记录) tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 (null) (null)
3 30 (null) (null)
其实以上结果的关键原因就是left join,right join,full join的特殊性,
不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。
** 而inner join没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。**
3. Hive实战--级联求和(累计报表)
需求:
有如下访客访问次数统计表 t_access_times
| 访客 | 月份 | 访问次数 |
|---|---|---|
| A | 2015-01 | 5 |
| A | 2015-01 | 15 |
| B | 2015-01 | 5 |
| A | 2015-01 | 8 |
| B | 2015-01 | 25 |
| A | 2015-01 | 5 |
| A | 2015-02 | 4 |
| A | 2015-02 | 6 |
| B | 2015-02 | 10 |
| B | 2015-02 | 5 |
需要输出报表:t_access_times_accumulate
月访问:当月的总次数;累计访问总计:截止到当月的月访问次数之和。
| 访客 | 月份 | 月访问总计 | 累计访问总计 |
|---|---|---|---|
| A | 2015-01 | 33 | 33 |
| A | 2015-02 | 10 | 43 |
| B | 2015-01 | 30 | 30 |
| B | 2015-02 | 15 | 45 |
准备数据:
A,2015-01,5
A,2015-01,15
B,2015-01,5
A,2015-01,8
B,2015-01,25
A,2015-01,5
A,2015-02,4
A,2015-02,6
B,2015-02,10
B,2015-02,5
create table t_access_time(username string,month string,salary int)
row format delimited fields terminated by ',';
load data local inpath '/home/hadoop/t_access_times.dat' into table t_access_time;
1、第一步,先求每个用户的月总金额
select username,month,sum(salary) from t_access_time group by username,month;
+-----------+----------+---------+--+
| username | month | salary |
+-----------+----------+---------+--+
| A | 2015-01 | 33 |
| A | 2015-02 | 10 |
| B | 2015-01 | 30 |
| B | 2015-02 | 15 |
+-----------+----------+---------+--+
2、第二步,将月总金额表 自己连接(自连接)
select * from
(select username,month,sum(salary) as salary from t_access_time group by username,month) TabA
inner join
(select username,month,sum(salary) as salary from t_access_time group by username,month) TabB
on TabA.username = TabB.username;
+-------------+----------+-----------+-------------+----------+-----------+--+
| a.username | a.month | a.salary | b.username | b.month | b.salary |
+-------------+----------+-----------+-------------+----------+-----------+--+
| A | 2015-01 | 33 | A | 2015-01 | 33 |
| A | 2015-01 | 33 | A | 2015-02 | 10 |
| A | 2015-02 | 10 | A | 2015-01 | 33 |
| A | 2015-02 | 10 | A | 2015-02 | 10 |
| B | 2015-01 | 30 | B | 2015-01 | 30 |
| B | 2015-01 | 30 | B | 2015-02 | 15 |
| B | 2015-02 | 15 | B | 2015-01 | 30 |
| B | 2015-02 | 15 | B | 2015-02 | 15 |
+-------------+----------+-----------+-------------+----------+-----------+--+
3、第三步,从上一步的结果中
进行分组查询,分组的字段是a.username a.month
求月累计值: 将b.month <= a.month的所有b.salary求和即可
select TabA.username,TabA.month,max(TabA.salary) as month_salary,sum(TabB.salary) as sum_salary
from
(select username,month,sum(salary) as salary from t_access_time group by username,month) TabA
inner join
(select username,month,sum(salary) as salary from t_access_time group by username,month) TabB
on TabA.username = TabB.username
where TabB.month<= TabA.month
group by TabA.username,TabA.month;
max(TabA.salary)不能直接写成TabA.salary,因为这个字段没有出现在group by中,也没有聚合函数,所以使用max表示。
结果:
A 2015-01 33 33
A 2015-02 10 43
B 2015-01 30 30
B 2015-02 15 45
参考http://www.w2b-c.com/article/150326(去掉-)
初接触,记下学习笔记,还有很多问题,望指导,谢谢。
HQL练习的更多相关文章
- hibernate -- HQL语句总结
1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = "from Users"; Query query ...
- Hibernate 查询方式(HQL/QBC/QBE)汇总
作为老牌的 ORM 框架,Hibernate 在推动数据库持久化层所做出的贡献有目共睹. 它所提供的数据查询方式也越来越丰富,从 SQL 到自创的 HQL,再到面向对象的标准化查询. 虽然查询方式有点 ...
- hql中in关键字的用法
hql: from " + FoodComment.class.getName() + " f where f.id in :groupIds" 封装的方法: publi ...
- [转]HQL中的子查询
原文地址:http://blog.csdn.net/xb12369/article/details/8638683 子查询: 子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一 ...
- Hibernate--------八大类HQL查询集合
Hibernate的 八大类HQL查询集合 Hibernate的八大类HQL查询集合: 一:属性查询(SimplePropertyQuery) 1,单一属性查询 *返回结果集属性列表,元素类型和实 ...
- Hibernate之HQL添加过滤器查询的用法
HQL查询过程中支持添加过滤器.使用步骤是这样的: 首先在要查询的实体对象的映射中使用<filter-def>标签配置过滤器,并在相对应的<class>标签中添加对应的< ...
- Hibernate之HQL查询的一些例子
Hibernate配备了一种非常强大的查询语言,就是HQL(hibernate query language),HQL看上去很像sql,但只是语法结构上相似,HQL是一种面向对象的查询,他可以理解继承 ...
- Hibernate —— HQL、QBC检索方式
一.HQL 检索方式 以双向的一对多来测试 HQL 检索方式.以 Department 和 Employee 为例. 建表语句: CREATE TABLE department ( dept_id ) ...
- hibernate中HQL练习时候一个小小的错误导致语法异常
package cn.db.po.test; import java.util.List; import cn.db.po.User; import cn.db.po.biz.UserBiz; pub ...
- [NHibernate]HQL查询
目录 写在前面 文档与系列文章 查询的几种方式 HQL查询 一个例子 总结 写在前面 上篇文章介绍了nhibernate在项目中的基本配置,包括数据库连接字符串的设置,映射文件的配置及需注意的地方,这 ...
随机推荐
- SQL点滴1—SET QUOTED_IDENTIFIER OFF语句的作用
原文:SQL点滴1-SET QUOTED_IDENTIFIER OFF语句的作用 先看下面几个sql语句 代码 SELECT * FROM [USER] WHERE a= 'netasp' ...
- 基于Asterisk的VoIP开发指南——(1)实现基本呼叫功能
原文:基于Asterisk的VoIP开发指南--(1)实现基本呼叫功能 说明: 1.本文档探讨基于Asterisk如何实现VoIP的一些基本功能,包括基本呼叫功能的方案选取.主叫号码透传.如何编写As ...
- VS代码生成工具ReSharper发布8.1版本
ReSharper是一个著名的VS代码生成工具,能帮助VS成为一个更佳的IDE.JetBrains公司今天发布了ReSharper最新版本8.1. 本次新版本更新涉及到打印稿.与VS2013集成.代码 ...
- 使用Windows2003创建DHCP服务器 - 进阶者系列 - 学习者系列文章
Windows 2003提供的DHCP服务还是挺强大的.下面大概介绍下DHCP服务器的配置. 1. 通过控制面板安装DHCP服务 2. 打开DHCP配置项 3. 选择 新建作用域 4. 输入名 ...
- cocos2d-x3.2中将XCode发展project转移到VS2010可能会发生错误
一些代码在线xcode写.我们希望我们自己的屌丝vs上述的实施,要重新构建它project,然后加载.但是绝对 没想到在VS里新建project再加入文件,编译后出现了好多错误.以下就把解决这些错误的 ...
- 仿async/await(一)and Gulp:新一代前端构建利器
NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C# ...
- iOS基础 - KVC and KVO
一.KVC(key value coding) 我们一般是通过调用set方法或属性的点语法来直接更改对象的状态,即对象的属性值,比如[stu setAge:10]; stu.age = 9; KVC ...
- [Usaco2008 Mar]Cow Travelling游荡的奶牛[简单DP]
Description 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John ...
- CODEFORCES#274 DIV2
A[傻逼题] 大意:给你a,b,c三个数,你可以在其中加上括号,加号,乘号,使得到的值最大 就是问你 a+b+c,a*(b+c),(a+b)*c,a*b*c,(a+c)*b 哪个最大! 我去...这不 ...
- LevelDB架构
LevelDB系列之整体架构 LevelDb本质上是一套存储系统以及在这套存储系统上提供的一些操作接口.为了便于理解整个系统及其处理流程,我们可以从两个不同的角度来看待LevleDb:静态角度和动 ...