Hive sql语句必练50题之31-40

31、查询1990年出生的学生名单:
select *
from student
where
year(s_birth)=‘1990’;

s_id s_name s_birth s_sex
01 赵雷 1990-01-01 男
02 钱电 1990-12-21 男
03 孙风 1990-05-20 男
04 李云 1990-08-06 男

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
select c_id,round(avg(s_score),2) avgscore
from score
group by c_id
order by avgscore desc,c_id asc;

c_id avgscore
02 72.67
03 68.5
01 64.5

?33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
select stu.s_id,stu.s_name,
avg(sc.s_score) avgScore
from student stu
join score sc on stu.s_id=sc.s_id
group by stu.s_id
having avgScore>=85;

select stu.s_id,stu.s_name,round(avg(sc.s_score),2) avgscore
from student stu
join score sc on stu.s_id=sc.s_id
group by stu.s_id
having round(avg(sc.s_score),2) >= 85
;

34、查询课程名称为"数学",且分数低于60的学生姓名和分数:
select stu.s_name,sc.s_score
from student stu
join course cs on cs.c_name=‘数学’
join score sc on sc.s_id=stu.s_id and cs.c_id=sc.c_id
where sc.s_score<60;

s_name s_score
李云 30

?35、查询所有学生的课程及分数情况:
select *
from student stu
join course cs
left join score sc on sc.s_id=stu.s_id and sc.c_id=cs.c_id ;
-------------------------部分截取--------------------------------
s_id s_name s_birth s_sex c_id c_name t_id s_id c_id s_score
01 赵雷 1990-01-01 男 01 语文 02 01 01 80
01 赵雷 1990-01-01 男 02 数学 01 01 02 90

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:
select stu.s_name,cs.c_name,sc.s_score
from student stu
join score sc on sc.s_id=stu.s_id
join course cs on cs.c_id=sc.c_id
where
sc.s_score>=70;

s_name c_name s_score
赵雷 语文 80
赵雷 数学 90
赵雷 英语 99
钱电 语文 70
钱电 英语 80
孙风 语文 80
孙风 数学 80
孙风 英语 80
周梅 语文 76
周梅 数学 87
郑竹 数学 89
郑竹 英语 98

37、查询课程不及格的学生:
select stu.*,sc.s_score
from student stu
join score sc
where
sc.s_score <60;
---------------------截取部分---------
s_id s_name s_birth s_sex s_score
01 赵雷 1990-01-01 男 50
01 赵雷 1990-01-01 男 30
01 赵雷 1990-01-01 男 20
01 赵雷 1990-01-01 男 31

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
select sc.s_id,stu.s_name
from student stu
join score sc on stu.s_id=sc.s_id
where sc.c_id=‘01’ and s_score=‘80’;

s_id s_name
01 赵雷
03 孙风

39、求每门课程的学生人数:
select c_id,
count(*) count
from score
group by c_id;

c_id count
01 6
02 6
03 6

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:
select *
from (
select stu.*,
row_number() over(distribute by sc.c_id sort by sc.s_score desc) rn
from student stu
join teacher ter on ter.t_name=‘张三’
join course cs on cs.t_id = ter.t_id
join score sc on sc.c_id = cs.c_id
)a
where a.rn==1;

s_id s_name s_birth s_sex rn
06 吴兰 1992-03-01 女 1

1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下 如果出现没有权限的问,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有表 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值