Hive经典案例:求出数学成绩大于语文成绩学生的信息

1、数据准备ui

有以下数据,学生id,课程,分数spa

1,yuwen,43
1,shuxue,55
2,yuwen,77
2,shuxue,88
3,yuwen,98
3,shuxue,65code

 

2、需求分析orm

一、建立表blog

create table requirement(
    sid int,
    course string,
    score int
)
row format delimited fields terminated by ',';

二、上传数据rem

load data local inpath '/usr/mydir/data/requirement.txt' into table requirement;

三、验证数据是否正确string

select * from requirement;

四、查询数据it

方法一:table

select 
    sid, 
    max(math) as math, 
    max(chinese) as chinese 
from (
select 
    *, 
    case course when 'shuxue' then score else 0 end as math,
    case course when 'yuwen' then score else 0 end as chinese
from requirement) t 
group by sid having math > chinese;

方法二:form

SELECT 
    a.sid,a.score math,b.score chinese
FROM 
    (select sid,course,score FROM requirement where course = 'shuxue') a
left join
    (select sid,course,score FROM requirement where course = 'yuwen') b
on a.sid = b.sid
where a.score >= b.score;

!注意:使用方法二进行链接查询,运行时所消耗的时间较多!

 

3、结果

相关文章
相关标签/搜索