以下数据的每一列分别表示:
sid name gender age academy dt chinese math english
95001 李勇 男 20 CS 2017-08-3156286295002 刘晨 女 19IS2017-08-3184223895003 王敏 女 22 MA 2017-08-3138808295004 张立 男 19IS2017-08-313965495005 刘刚 男 18 MA 2018-08-3118287995006 孙庆 男 23 CS 2018-08-3112671695007 易思玲 女 19 MA 2018-08-3127608495008 李娜 女 18 CS 2018-08-3112128295009 梦圆圆 女 18 MA 2018-08-3162359495010 孔小涛 男 19 CS 2017-08-318297495011 包小柏 男 18 MA 2019-08-3141537195012 孙花 女 20 CS 2017-08-3190517995013 冯伟 男 21 CS 2019-08-3120698695014 王小丽 女 19 CS 2017-08-3183646095015 王君 男 18 MA 2019-08-3139482995016 钱国 男 21 MA 2019-08-319476995017 王风娟 女 18IS2019-08-3187565495018 王一啊 女 19IS2019-08-3154362495019 邢小丽 女 19IS2018-08-317898295020 赵钱 男 21IS2019-08-314488795021 周二 男 17 MA 2018-08-3184611695022 郑明 男 20 MA 2018-08-3164709095023 李小勇 男 20 CS 2017-08-3152211995024 刘小晨 女 19IS2017-08-311167695025 王小敏 女 22 MA 2017-08-3113899895026 张小立 男 19IS2017-08-3185848095027 刘小刚 男 18 MA 2018-08-317174895028 孙小庆 男 23 CS 2018-08-3117948395029 易小思玲 女 19 MA 2018-08-3169842395030 李小娜 女 18 CS 2018-08-3116707095031 梦小圆 女 18 MA 2018-08-3182483795032 孔涛 男 19 CS 2017-08-3183801295033 包柏 男 18 MA 2019-08-3118167195034 孙小花 女 20 CS 2017-08-3136959395035 冯小伟 男 21 CS 2019-08-3131191395036 王丽 女 19 CS 2017-08-311642995037 王小君 男 18 MA 2019-08-319136795038 钱小国 男 21 MA 2019-08-3189591895039 王娟 女 18IS2019-08-317176795040 王小一 女 19IS2019-08-3149324595041 邢丽 女 19IS2018-08-3130404095042 赵小钱 男 21IS2019-08-3171695795043 周小二 男 17 MA 2018-08-31189195044 郑一明 男 20 MA 2018-08-316434495045 张一勇 男 20 CS 2017-08-3110659795046 刘一丽 女 19IS2017-08-315684595047 张一敏 女 22 MA 2017-08-311989895048 张一立 男 19IS2017-08-3149399295049 刘一刚 男 18 MA 2018-08-314799195050 孙一庆 男 23 CS 2018-08-3199716195051 易一玲 女 19 MA 2018-08-3149537195052 李一 女 18 CS 2018-08-3122877995053 小一 女 18 MA 2018-08-3153583595054 孔一 男 19 CS 2017-08-313062595055 包一 男 18 MA 2019-08-3173682895056 孙一庆 女 20 CS 2017-08-3168223395057 冯一 男 21 CS 2019-08-3191264695058 王一二 女 19 CS 2017-08-3149815195059 王一三 男 18 MA 2019-08-3156274995060 钱一 男 21 MA 2019-08-3197402495061 王小娟 女 18IS2019-08-3196793495062 王小一 女 19IS2019-08-318765595063 邢丽 女 19IS2018-08-3156193395064 赵小钱 男 21IS2019-08-3170178595065 周小二 男 17 MA 2018-08-3166191495066 郑小明 男 20 MA 2018-08-3199951495067 李勇 男 19 CS 2017-08-3148401395068 刘晨 女 19IS2017-08-3186284695069 王敏 女 21 MA 2017-08-319159095070 张立 男 17IS2017-08-3199396495071 刘刚 男 20 MA 2018-08-317326495072 孙庆 男 19 CS 2018-08-319071595073 易思玲 女 19 MA 2018-08-3172872595074 李娜 女 21 CS 2018-08-3192236595075 梦圆圆 女 17 MA 2018-08-313734595076 孔小涛 男 20 CS 2017-08-3115374095077 包小柏 男 19 MA 2019-08-3129823095078 孙花 女 19 CS 2017-08-316277595079 冯伟 男 21 CS 2019-08-314102995080 王小丽 女 17 CS 2017-08-3170703695081 王君 男 20 MA 2019-08-3120743195082 钱国 男 19 MA 2019-08-3159853995083 王风娟 女 19IS2019-08-316989595084 王一 女 21IS2019-08-3116262795085 邢小丽 女 17IS2018-08-317328495086 赵钱 男 20IS2019-08-3169525995087 周二 男 19 MA 2018-08-3129401095088 郑明 男 18 MA 2018-08-3177139195089 李勇 男 23 CS 2017-08-314922895090 刘晨 女 19IS2017-08-3112182595091 王敏 女 18 MA 2017-08-3144944795092 张立 男 18IS2017-08-3171451795093 刘刚 男 19 MA 2018-08-3127905095094 孙庆 男 18 CS 2018-08-3192347595095 易思玲 女 20 MA 2018-08-3138632495096 李娜 女 21 CS 2018-08-3194663795097 梦圆圆 女 19 MA 2018-08-3191354595098 孔小涛 男 18 CS 2017-08-31961095099 包小柏 男 19 MA 2019-08-3193662595100 孙花 女 18 CS 2017-08-3147222795101 冯伟 男 23 CS 2019-08-3123996295102 王小丽 女 19 CS 2017-08-31634395103 王君 男 18 MA 2019-08-3107895104 钱国 男 18 MA 2019-08-319323595105 王风 女 19IS2019-08-3112845095106 王一 女 18IS2019-08-3129608695107 邢小 女 20IS2018-08-315051695108 赵钱 男 21IS2019-08-3193548695109 周二 男 19 MA 2018-08-3157399395110 郑明 男 18 MA 2018-08-3130786295111 张勇 男 19 CS 2017-08-312551995112 刘丽 女 18IS2017-08-3174964495113 张敏 女 23 MA 2017-08-3164178295114 张小 男 19IS2017-08-3114576995115 刘小 男 18 MA 2018-08-3137842995116 孙小 男 18 CS 2018-08-317992695117 易小 女 19 MA 2018-08-3160972695118 李小 女 18 CS 2018-08-315788895119 小小 女 20 MA 2018-08-317976495120 孔大 男 21 CS 2017-08-3133278595121 包大 男 19 MA 2019-08-319623895122 孙小 女 18 CS 2017-08-3154942395123 冯小伟 男 19 CS 2019-08-312645195124 王小丽 女 18 CS 2017-08-3165774395125 王小君 男 23 MA 2019-08-3124223495126 钱小国 男 19 MA 2019-08-319898295127 王小娟 女 18IS2019-08-3136663095128 王小一 女 18IS2019-08-3198781595129 邢丽 女 19IS2018-08-3141415295130 赵小钱 男 18IS2019-08-3187659395131 周小二 男 20 MA 2018-08-3126564895132 郑小明 男 21 MA 2018-08-3131966095133 李勇 男 19 CS 2017-08-3126537895134 刘晨 女 18IS2017-08-3160271195135 王敏 女 19 MA 2017-08-3163256095136 张立 男 18IS2017-08-3146867795137 刘刚 男 23 MA 2018-08-317464895138 孙庆 男 19 CS 2018-08-3118662095139 易思玲 女 18 MA 2018-08-3172874495140 李娜 女 18 CS 2018-08-3112685595141 梦圆圆 女 19 MA 2018-08-3163702995142 孔小涛 男 18 CS 2017-08-311401595143 包小柏 男 20 MA 2019-08-312978395144 孙花 女 21 CS 2017-08-311218695145 冯d伟 男 19 CS 2019-08-3151868595146 王3丽 女 18 CS 2017-08-3188331195147 王d君 男 23 MA 2019-08-3161982995148 钱1国 男 19 MA 2019-08-3188651295149 王2娟 女 18IS2019-08-3130644195150 王B一 女 18IS2019-08-3116931095151 邢A丽 女 19IS2018-08-3158202495152 赵钱D 男 18IS2019-08-3177489295153 周二C 男 20 MA 2018-08-318976095154 郑明B 男 21 MA 2018-08-3118765995155 李勇A 男 19 CS 2017-08-31533549
将上面的数据存储到一个分区分桶表中。要求:分区按照学院和入学日期分区,分四个桶。
1、建表语句
-- 创建临时表droptableifexists student_temp;createtableifnotexists student_temp(
sid int,
name string,
gender char(1),
age int,
academy string,
dt date,
chinese int,
math int,
english int)row format delimited
fieldsterminatedby'\t';-- 加载数据loaddatalocal inpath './data/student_temp.txt'intotable student_temp;createtable student_info(
sid int,
name string,
gender string,
age int,
chinese int,
math int,
english int)
partitioned by(academy string,dt date)clusteredby(sid) sorted by(age)into4 buckets
row format delimited
fieldsterminatedby'\t';2、动态导入语句
```sql
insert into student_info partition(academy,dt) select sid,
name,gender,age,chinese,math,english,academy,dt from temp_student_info
distribute by (sid) sort by (age desc);
3、统计每个学院每年入学的总人数以及前一年的总人数。
select
academy,aa,`总人数`,lag(`总人数`,1) over(distribute by academy sort by aa) `去年总人数`
from
(
select
academy,year(dt) aa,count(1) `总人数`
from student_info
group by academy,year(dt)
) a
;
4、统计每个学院的男女人数,在一行上显示,并按照学院名称升序。
查询效果如下:
学院 男 女
cs xxx xxx
MA xxxx xxx
select academy,
sum(case when gender='男' then 1 else 0 end) `男`,
sum(case when gender='女' then 1 else 0 end) `女`
from student_info
group by academy
;
5、查询每个学院的每年入学总成绩排名的前五名的学生信息:连续,重复效果
select
*
from
(
select
*,dense_rank() over(distribute by academy,year(dt) sort by `总成绩`) aa
from
(
select
*,chinese+math+english `总成绩`from student_info
) a
) b
where b.aa<6;
2、
a_test、b_test、c_test三个hive表 每个表中都只有一列int类型且列名相同,求三个表中互不重复的数
a.txt b.txt c.txt
1212323113412115145615671678188935206307408createtable a_test(
id int);loaddatalocal inpath './data/a.txt'intotable a_test;createtable b_test(
id int);createtable c_test(
id int);
答案:
select
id
from(select id from a
unionALLselect id from b
unionALLselect id from c
)t1
groupby id
HAVINGcount(1)=1
3、
数据:
content_test表:
uid contents
1 i|love|china
2 china|is|good|i|i|like
需求:词频统计。统计结果如下,如果出现次数一样,则按照content名称排序:
content cnt
i 3
china 2
good 1like1
love 1is1createtable content_test(
uid int,
contents string
)row format delimited fieldsterminatedby' ';loaddatalocal inpath './data/content_test'intotable content_test;
答案:
select b.a `content`,count(1)`cnt`from(select explode(split(contents,'\\|')) a
from content_test
) b
groupby b.a
;
4、
需求:求出连续七天登陆的总人数
数据: login_test表
uid dt login_status(1登录成功,0异常)12019-07-11112019-07-12112019-07-13112019-07-14112019-07-15112019-07-16112019-07-17112019-07-18122019-07-11122019-07-12122019-07-13022019-07-14122019-07-15122019-07-16022019-07-17122019-07-18032019-07-11132019-07-12132019-07-13132019-07-14132019-07-15132019-07-16132019-07-17132019-07-181createtable login_test(
uid int,
dt string,
login_status int)row format delimited fieldsterminatedby'\t';loaddatalocal inpath './data/login_test'intotable login_test;
答案:
selectcount(distinct b.uid)from(select*, datediff(dt,lag(dt,6)over(distribute by uid sort by dt)) a
from login_test
where login_status=1) b
where b.a=6;
5、
2014010114201401021620140103172014010410201401050620120106092012010732201201081220120109192012011023200101011620010102122001010310200101041120010105292013010619201301072220130108122013010929201301102320080101052008010216200801033720080104142008010516200701061920070107122007010812200701099920070110232010010114201001021620100103172010010410201001050620150106492015010722201501081220150109992015011023createtableifnotexists temperature(data string
)row format delimited fieldsterminatedby'\t'linesterminatedby'\n';loaddatalocal inpath './data/temperature'intotable temperature;
数据解析:比如:2010012325表示在2010年01月23日的气温为25度。
需求:现在要求使用hive,计算每一年的最大气温的日期+温度。效果如下:
20010105292007010999200801033720100103172012010732201301092920140103172015010999
答案
select
b.dt,b.tempfrom(select
a.year,a.dt,a.temp,row_number()over(distribute by a.year sort by a.tempdesc) rn
from(select
substr(data,0,4)year,substr(data,0,8) dt,substr(data,9,2)tempfrom temperature
) a
) b
where b.rn=1;
6、
login_user_test表
uid,udate
1,2019-08-011,2019-08-021,2019-08-032,2019-08-012,2019-08-023,2019-08-013,2019-08-034,2019-07-284,2019-07-294,2019-08-014,2019-08-024,2019-08-03createtable login_user_test(
uid int,
udate date)row format delimited
fieldsterminatedby',';loaddatalocal inpath './data/login_user_test.txt'intotable login_user_test;
需求:查询每个用户连续登陆的最大天数,结果如下:
uid cnt_days
13223143
答案
select
uid,max(days)from(select
uid,dt,count(1) days
from(select
uid,date_sub(udate,row_number()over(distribute by uid sort by udate)) dt --妙啊from login_user_test
) a
groupby uid,dt
) b
groupby uid
;
user_hobbys表,数据如下、数据是制表符隔开的:
john 男 打乒乓球,游泳,看电影
jack 男 打乒乓球,看电影
lucy 女 看书,看电影
rose 女 看电影,大乒乓球
lili 男 足球,看电影,大乒乓球
smith 男 游泳,篮球,大乒乓球
Asia 女 看书,看电影
linus 女 看书
表名user_hobbys,字段名 name sex hobby
createtableifnotexists user_hobbys(
name string,
sex string,
hobby array<string>)row format delimited
fieldsterminatedby'\t'
collection items terminatedby',';loaddatalocal inpath './data/user_hobbys.txt'intotable user_hobbys;
需求:查询最被喜欢的爱好的前三名使用hql实现结果如下:
答案:
select B.hobb from(select A.hobb,A.num,dense_rank()over(orderby A.num desc) rk
from(select hobb,count(*) num from user_hobbys lateral view explode(hobby) t as hobb groupby hobb orderby num desc) A ) B where B.rk<4;
9、
lili 女 chinese:90,math:87,english:63,nature:76
lucy 女 chinese:60,math:30,english:78,nature:0
jack 男 chinese:89,math:25,english:81,nature:9
john 男 chinese:45,math:90,english:100,nature:80
mack 男 chinese:67,math:20,english:90,nature:45
tick 男 chinese:89,math:56,english:82,nature:34
rose 女 chinese:30,math:65,english:56,nature:76
hong 女 chinese:54,math:60,english:90,nature:56
xiao 女 chinese:66,math:70,english:12,nature:99
完成建表语句:表名:students_test,字段 name,gender,scores,制表符分隔符
createtable students_test(
name string,
gender string,
scores map<string,int>)row format delimited
fieldsterminatedby'\t'
collection items terminatedby','
map keysterminatedby':';loaddatalocal inpath './data/students_test.txt'intotable students_test;
需求1:查询男生第二名和女生第二名的姓名、性别、总成绩、以及平均分(并列连续效果)
select B.name,B.gender,B.totalscore,B.avgscore
from(select A.name,A.gender,A.totalscore,A.avgscore,dense_rank()over(partitionby gender orderby totalscore desc) rk
from(select name,gender,sum(score) totalscore,avg(score) avgscore
from students_test lateral view explode(scores) t as subject,score
groupby name,gender) A )B where B.rk=2;select*from t_user where uname like'%u%'and sal >2000
10、
准备数据:
name constellation blood_type
小明 小熊座 R
小红 猎户座 XR
小白 猎户座 R
小蓝 小熊座 R
小绿 小熊座 R
建表,导入数据
createtable white(
name string,
constellation string,
blood_type string
)row format delimited
fieldsterminatedby'\t';loaddatalocal inpath '/root/hivedata/white'intotable white;
需求:
1、将星座和血型相同的人显示(行转列:多行)
select
t.base,concat_ws('|',collect_set(t.name)) name
from(select*,concat(constellation,',',blood_type) base
from white
) t
groupby t.base
;
11、
编写sql答案
数据:
userid,month,visits
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
A,2015-03,16
A,2015-03,22
B,2015-03,23
B,2015-03,10
B,2015-03,1
每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数,结果数据格式如下:
uid|month|current_month_sum|截止到每月为止的最大单月访问次数|累计到本月的总访问次数|---|-------|-----------------|----------------|-----------|
A |2015-01|33|33|33|
A |2015-02|10|33|43|
A |2015-03|38|38|81|
B |2015-01|30|30|30|
B |2015-02|15|30|45|
B |2015-03|34|34|79|
答案:
select uid,month,current_month_sum,max(current_month_sum)over(distribute by uid sort bymonth)`截止到每月为止的最大单月访问次数`,sum(current_month_sum)over(distribute by uid sort bymonth)`累计到本月的总访问次数`from(select
uid,month,sum(visits) current_month_sum
from maxvisits
groupby uid,month) a
;
12、
数据: t4表:
id tag flag
a b 2
a b 1
a b 3
c d 6
c d 8
c d 8
编写sql实现如下结果:
id tag flag
a b 1|2|3
c d 6|8createtable t4(
id string,
tag string,
flag int)row format delimited
fieldsterminatedby' ';loaddatalocal inpath '/root/hivedata/t4.txt' overwrite intotable t4;select
id,tag,concat_ws("|",collect_set(cast(flag as string))) flag
from t4
groupby id,tag
;
connect_set()函数,必须是string类型或者array类型才能使用,所以应该将要收集的字段修改为string类型,否则会报以下错误
Argument type mismatch 'flag': Argument 2offunction CONCAT_WS must be "string or array<string>
13、
数据: T2表:
Tags
1,2,31,22,3
T3表:
id lab
1 A
2 B
3 C
根据T2和T3表的数据,编写sql实现如下结果:
+--------+--------+--+| tags | labs |+--------+--------+--+|1,2| A,B ||1,2,3| A,B,C ||2,3| B,C |+--------+--------+--+createtable t2(
tags string
);loaddatalocal inpath '/root/hivedata/t2.txt' overwrite intotable t2;createtable t3(
id int,
lab string
)row format delimited
fieldsterminatedby' ';loaddatalocal inpath '/root/hivedata/t3.txt' overwrite intotable t3;select
tags,
concat_ws(",",collect_set(lab)) labs
from(select*from t3
join(select
tags,tag
from t2 lateral view explode(split(tags,","))t as tag
) a
on a.tag=t3.id
) b
groupby b.tags
;
14、
数据: t5表
uid name tags
1 goudan chihuo,huaci
2 mazi sleep
3 laotie paly
编写sql实现如下结果:
uid name tag
1 goudan chihuo
1 goudan huaci
2 mazi sleep
3 laotie paly
createtable t5(
uid string,
name string,
tags string
)row format delimited
fieldsterminatedby'\t';loaddatalocal inpath '/root/hivedata/t5.txt' overwrite intotable t5;select
uid,name,tag
from t5 lateral view explode(split(tags,","))t as tag
;
15、
数据: content表:
uid contents
1 i|love|china
2 china|is|good|i|i|like
统计结果如下,如果出现次数一样,则按照content名称排序:
+----------+------+--+| content | num |+----------+------+--+| i |3|| china |2|| good |1||is|1||like|1|| love |1|+----------+------+--+droptable content;createtable content_test(
uid int,
contents string
)row format delimited
fieldsterminatedby'\t';loaddatalocal inpath '/root/hivedata/content_test' overwrite intotable content;select
content,count(1)as num
from(select*from content_test lateral view explode(split(contents,"\\|")) t as content
) a
groupby content
;
注意:|符号要记得转义!!!
explode()函数只支持展开map类型和array类型
explode() takes an array or a map as a parameter
split()函数是用于切分数据,也就是将一串字符串切割成了一个数组,标准格式为split(str,regex),支持正则切分不过
有点奇怪的是,对于 “.”,"|“这样的特殊字符,不加”\“的时候是特殊字符,加了以后才是普通字符,而对于”\d"的字符,需要
加"\“后才是特殊字符,就是是说”\\d"才是匹配数字。
16、
数据: course1表
id course
1,a
1,b
1,c
1,e
2,a
2,c
2,d
2,f
3,a
3,b
3,c
3,e
根据编写sql,得到结果如下(表中的1表示选修,表中的0表示未选修):
+-----+----+----+----+----+----+----+--+| id | a | b | c | d | e | f |+-----+----+----+----+----+----+----+--+|1|1|1|1|0|1|0||2|1|0|1|1|0|1||3|1|1|1|0|1|0|+-----+----+----+----+----+----+----+--+createtable course(
id int,
course string
)row format delimited
fieldsterminatedby',';loaddatalocal inpath '/root/hivedata/course.txt' overwrite intotable course;select
id,sum(if(course='a',1,0))as a,sum(if(course='b',1,0))as b,sum(if(course='c',1,0))as c,sum(if(course='d',1,0))as d,sum(if(course='e',1,0))as e,sum(if(course='f',1,0))as f
from course
groupby id
;
要加sum()函数,否则后面的别名对应的是一列数据而不是一个,所以会报错