hive 练习
Posted jeasonit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive 练习相关的知识,希望对你有一定的参考价值。
1 练习题和sql语句 2 3 基础sql语句 4 5 统计每个学生的总分。 6 统计出学号和总分 7 select s_id , sum(s_score) as zf from score group by s_id 8 9 上表结果和stuent做join连接 10 11 select stu.s_name,zf.ss from 12 13 (select s_id , sum(s_score) as ss from score group by s_id) as zf 14 15 Join 16 17 student as stu 18 19 on stu.s_id=zf.s_id; 20 2.查询表中所有学生的姓名和对应的英语成绩。 21 22 select stu.s_name,c.c_name,s.s_score from 23 24 score as s 25 26 join 27 28 course as c 29 30 on s.c_id=c.c_id 31 32 join 33 34 student as stu 35 36 on stu.s_id = s.s_id 37 38 where 39 40 c.c_name = ‘english‘; 41 3. 在所有学生总分数上加10分特长分。 42 43 关键代码 44 45 select s_id,sum(s_score)+10 from score group by s_id; 46 47 查询姓名为liyun的学生总成绩 48 select sum(s_score) from score where s_id in (select s_id from student where s_name=‘liyun‘); 49 50 查询英语成绩大于90分的同学 51 52 53 54 8.查询总分大于270分的所有同学 55 56 57 58 查询英语分数在 80-90之间的同学。 59 60 61 查询数学分数为89,90,91的同学。 62 63 64 查询所有姓li的学生平均成绩。 65 66 67 查询数学分>80,语文分>80的同学。 68 69 70 对数学成绩排序后输出。 71 72 73 对总分排序后输出,然后再按从高到低的顺序输出 74 75 76 对姓li的学生数学成绩排序输出 77 78 79 80 81 有一定难度的sql语句 82 83 84 85 -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 86 思路: 87 关键步骤: 88 select * from score l join score r on (l.s_id = r.s_id and l.c_id=2 and r.c_id=1); 89 90 select a.* ,b.s_score as 01_score,c.s_score as 02_score from 91 student a 92 join score b on a.s_id=b.s_id and b.c_id=‘01‘ 93 left join score c on a.s_id=c.s_id and c.c_id=‘02‘ or c.c_id = NULL where b.s_score>c.s_score 94 -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 95 select a.* ,b.s_score as 01_score,c.s_score as 02_score from 96 student a left join score b on a.s_id=b.s_id and b.c_id=‘01‘ or b.c_id=NULL 97 join score c on a.s_id=c.s_id and c.c_id=‘02‘ where b.s_score<c.s_score 98 -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 99 select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 100 student b 101 join score a on b.s_id = a.s_id 102 GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60; 103 -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 104 -- (包括有成绩的和无成绩的) 105 select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 106 student b 107 left join score a on b.s_id = a.s_id 108 GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60 109 unionselect a.s_id,a.s_name,0 as avg_score from 110 student a 111 where a.s_id not in ( 112 select distinct s_id from score); 113 -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 114 select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from 115 student a 116 left join score b on a.s_id=b.s_id 117 GROUP BY a.s_id,a.s_name; 118 -- 6、查询"李"姓老师的数量 119 select a.* from 120 student a 121 join score b on a.s_id=b.s_id where b.c_id in( 122 select c_id from course where t_id =( 123 select t_id from teacher where t_name = ‘张三‘)); 124 -- 7、查询学过"张三"老师授课的同学的信息 125 select a.* from 126 student a 127 join score b on a.s_id=b.s_id where b.c_id in( 128 select c_id from course where t_id =( 129 select t_id from teacher where t_name = ‘张三‘)); 130 -- 8、查询没学过"张三"老师授课的同学的信息 131 select * from student c 132 where c.s_id not in( 133 select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( 134 select c_id from course where t_id =( 135 select t_id from teacher where t_name = ‘张三‘)));-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 136 select a.* from 137 student a,score b,score c 138 where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id=‘01‘ and c.c_id=‘02‘; 139 -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 140 select a.* from 141 student a 142 where a.s_id in (select s_id from score where c_id=‘01‘ ) and a.s_id not in(select s_id from score where c_id=‘02‘) 143 -- 11、查询没有学全所有课程的同学的信息 144 select s.* from 145 student s where s.s_id in( 146 select s_id from score where s_id not in( 147 select a.s_id from score a 148 join score b on a.s_id = b.s_id and b.c_id=‘02‘ 149 join score c on a.s_id = c.s_id and c.c_id=‘03‘ 150 where a.c_id=‘01‘)) 151 -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 152 select * from student where s_id in( 153 select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id=‘01‘) 154 ); 155 -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 156 select a.* from student a where a.s_id in( 157 select distinct s_id from score where s_id!=‘01‘ and c_id in(select c_id from score where s_id=‘01‘) 158 group by s_id 159 having count(1)=(select count(1) from score where s_id=‘01‘));-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 select a.s_name from student a where a.s_id not in ( 160 select s_id from score where c_id = 161 (select c_id from course where t_id =( 162 select t_id from teacher where t_name = ‘张三‘)) 163 group by s_id); 164 -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 165 select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from 166 student a 167 left join score b on a.s_id = b.s_id 168 where a.s_id in( 169 select s_id from score where s_score<60 GROUP BY s_id having count(1)>=2) 170 GROUP BY a.s_id,a.s_name 171 -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息select a.*,b.c_id,b.s_score from 172 student a,score b 173 where a.s_id = b.s_id and b.c_id=‘01‘ and b.s_score<60 ORDER BY b.s_score DESC; 174 -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 175 select a.s_id,(select s_score from score where s_id=a.s_id and c_id=‘01‘) as 语文, 176 (select s_score from score where s_id=a.s_id and c_id=‘02‘) as 数学, 177 (select s_score from score where s_id=a.s_id and c_id=‘03‘) as 英语, 178 round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC; 179 -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90select 180 a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2), 181 ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率, 182 ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率, 183 ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率, 184 ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率 185 from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name 186 -- 19、按各科成绩进行排序,并显示排名(实现不完全) 187 -- mysql没有rank函数 188 select a.s_id,a.c_id, 189 @i:=@i +1 as i保留排名, 190 @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名, 191 @score:=a.s_score as score 192 from ( 193 select s_id,c_id,s_score from score WHERE c_id=‘01‘ GROUP BY s_id,c_id,s_score ORDER BY s_score DESC 194 )a,(select @k:=0,@i:=0,@score:=0)s 195 union 196 select a.s_id,a.c_id, 197 @i:=@i +1 as i, 198 @k:=(case when @score=a.s_score then @k else @i end) as rank, 199 @score:=a.s_score as score 200 from ( 201 select s_id,c_id,s_score from score WHERE c_id=‘02‘ GROUP BY s_id,c_id,s_score ORDER BY s_score DESC 202 )a,(select @k:=0,@i:=0,@score:=0)s 203 union 204 select a.s_id,a.c_id, 205 @i:=@i +1 as i, 206 @k:=(case when @score=a.s_score then @k else @i end) as rank, 207 @score:=a.s_score as score 208 from ( 209 select s_id,c_id,s_score from score WHERE c_id=‘03‘ GROUP BY s_id,c_id,s_score ORDER BY s_score DESC 210 )a,(select @k:=0,@i:=0,@score:=0)s 211 -- 20、查询学生的总成绩并进行排名select a.s_id, 212 @i:=@i+1 as i, 213 @k:=(case when @score=a.sum_score then @k else @i end) as rank, 214 @score:=a.sum_score as scorefrom (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a, 215 (select @k:=0,@i:=0,@score:=0)s 216 -- 21、查询不同老师所教不同课程平均分从高到低显示 217 select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a 218 left join score b on a.c_id=b.c_id 219 left join teacher c on a.t_id=c.t_id 220 GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 221 222 223 select d.*,c.排名,c.s_score,c.c_id from ( 224 select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id=‘01‘ 225 )c 226 left join student d on c.s_id=d.s_id 227 where 排名 BETWEEN 2 AND 3 228 UNION 229 select d.*,c.排名,c.s_score,c.c_id from ( 230 select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id=‘02‘ 231 )c 232 left join student d on c.s_id=d.s_id 233 where 排名 BETWEEN 2 AND 3 234 UNION 235 select d.*,c.排名,c.s_score,c.c_id from ( 236 select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id=‘03‘ 237 )c 238 left join student d on c.s_id=d.s_id 239 where 排名 BETWEEN 2 AND 3; 240 -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 241 select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from score a 242 left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`, 243 ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比 244 from score GROUP BY c_id)b on a.c_id=b.c_id 245 left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`, 246 ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比 247 from score GROUP BY c_id)c on a.c_id=c.c_id 248 left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`, 249 ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比 250 from score GROUP BY c_id)d on a.c_id=d.c_id 251 left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`, 252 ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比 253 from score GROUP BY c_id)e on a.c_id=e.c_id 254 left join course f on a.c_id = f.c_id 255 -- 24、查询学生平均成绩及其名次 256 select a.s_id, 257 @i:=@i+1 as ‘不保留空缺排名‘, 258 @k:=(case when @avg_score=a.avg_s then @k else @i end) as ‘保留空缺排名‘, 259 @avg_score:=avg_s as ‘平均分‘ 260 from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id)a,(select @avg_score:=0,@i:=0,@k:=0)b; 261 262 -- 25、查询各科成绩前三名的记录 263 -- 1.选出b表比a表成绩大的所有组 264 -- 2.选出比当前id成绩大的 小于三个的 265 select a.s_id,a.c_id,a.s_score from score a 266 left join score b on a.c_id = b.c_id and a.s_score<b.s_score 267 group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3 268 ORDER BY a.c_id,a.s_score DESC 269 -- 26、查询每门课程被选修的学生数 270 select c_id,count(s_id) from score a GROUP BY c_id 271 -- 27、查询出只有两门课程的全部学生的学号和姓名 272 select s_id,s_name from student where s_id in( 273 select s_id from score GROUP BY s_id HAVING COUNT(c_id)=2); 274 -- 28、查询男生、女生人数 275 select s_sex,COUNT(s_sex) as 人数 from student GROUP BY s_sex 276 -- 29、查询名字中含有"风"字的学生信息 277 select * from student where s_name like ‘%风%‘; 278 -- 30、查询同名同性学生名单,并统计同名人数 279 select a.s_name,a.s_sex,count(*) from student a JOIN 280 student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex 281 GROUP BY a.s_name,a.s_sex 282 -- 31、查询1990年出生的学生名单 283 select s_name from student where s_birth like ‘1990%‘ 284 -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 285 select c_id,ROUND(AVG(s_score),2) as avg_score from score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC 286 -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 287 select a.s_id,b.s_name,ROUND(avg(a.s_score),2) as avg_score from score a 288 left join student b on a.s_id=b.s_id GROUP BY s_id HAVING avg_score>=85 289 -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 290 291 select a.s_name,b.s_score from score b LEFT JOIN student a on a.s_id=b.s_id where b.c_id=( 292 select c_id from course where c_name =‘数学‘) and b.s_score<60 293 294 295 -- 35、查询所有学生的课程及分数情况; 296 select a.s_id,a.s_name, 297 SUM(case c.c_name when ‘语文‘ then b.s_score else 0 end) as ‘语文‘, 298 SUM(case c.c_name when ‘数学‘ then b.s_score else 0 end) as ‘数学‘, 299 SUM(case c.c_name when ‘英语‘ then b.s_score else 0 end) as ‘英语‘, 300 SUM(b.s_score) as ‘总分‘ 301 from student a left join score b on a.s_id = b.s_id 302 left join course c on b.c_id = c.c_id 303 GROUP BY a.s_id,a.s_name 304 -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 305 select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id 306 left join student a on a.s_id=c.s_id where c.s_score>=70 307 -- 37、查询不及格的课程 308 select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id 309 where a.s_score<60 310 --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 311 select a.s_id,b.s_name from score a LEFT JOIN student b on a.s_id = b.s_id 312 where a.c_id = ‘01‘ and a.s_score>80 313 -- 39、求每门课程的学生人数 314 select count(*) from score GROUP BY c_id; 315 316 -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 317 -- 查询老师id 318 319 select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name=‘张三‘ 320 -- 查询最高分(可能有相同分数) 321 select MAX(s_score) from score where c_id=‘02‘ 322 -- 查询信息 323 select a.*,b.s_score,b.c_id,c.c_name from student a 324 LEFT JOIN score b on a.s_id = b.s_id 325 LEFT JOIN course c on b.c_id=c.c_id 326 where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name=‘张三‘) 327 and b.s_score in (select MAX(s_score) from score where c_id=‘02‘) 328 -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 329 select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score 330 -- 42、查询每门功成绩最好的前两名 331 -- 牛逼的写法 332 select a.s_id,a.c_id,a.s_score from score a 333 where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id 334 -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 335 select c_id,count(*) as total from score GROUP BY c_id HAVING total>5 ORDER BY total,c_id ASC 336 -- 44、检索至少选修两门课程的学生学号 337 select s_id,count(*) as sel from score GROUP BY s_id HAVING sel>=2 338 -- 45、查询选修了全部课程的学生信息 339 select * from student where s_id in( 340 select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course)) 341 --46、查询各学生的年龄 342 -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 343 344 select s_birth,(DATE_FORMAT(NOW(),‘%Y‘)-DATE_FORMAT(s_birth,‘%Y‘) - 345 (case when DATE_FORMAT(NOW(),‘%m%d‘)>DATE_FORMAT(s_birth,‘%m%d‘) then 0 else 1 end)) as age 346 from student; 347 -- 47、查询本周过生日的学生 348 349 select * from student where WEEK(DATE_FORMAT(NOW(),‘%Y%m%d‘))=WEEK(s_birth) 350 select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),‘%Y%m%d‘)) 351 352 select WEEK(DATE_FORMAT(NOW(),‘%Y%m%d‘)) 353 -- 48、查询下周过生日的学生 354 select * from student where WEEK(DATE_FORMAT(NOW(),‘%Y%m%d‘))+1 =WEEK(s_birth) 355 -- 49、查询本月过生日的学生 356 select * from student where MONTH(DATE_FORMAT(NOW(),‘%Y%m%d‘)) =MONTH(s_birth) 357 -- 50、查询下月过生日的学生 358 select * from student where MONTH(DATE_FORMAT(NOW(),‘%Y%m%d‘))+1 =MONTH(s_birth) 359
以上是关于hive 练习的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段