![Hive的考试练习_第1张图片](http://img.e-com-net.com/image/info8/586d7d30413741fd9f18db56913db40d.jpg)
建表:
create table hive1
(name string, course string, score int)
row format delimited fields terminated by ',';
load data local inpath '/home/potter/hive1.txt'
into table hive1;
第一小题:
(1)
select name
from hive1 group by name having min(score) > 80;
![Hive的考试练习_第2张图片](http://img.e-com-net.com/image/info8/f05678a3a79747ecbf8661a31fe9ec37.png)
第二小题:
(2)
select name
from hive1 where score < 60 group by name;
![Hive的考试练习_第4张图片](http://img.e-com-net.com/image/info8/04ad7baf38e74f5cb6692cf089c856c5.jpg)
先建表:
CREATE TABLE `hive2` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(255) DEFAULT NULL,
`math` int(11) DEFAULT NULL,
`computer` int(11) DEFAULT NULL,
`english` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hive2` VALUES (1, 'huangbo', 34, 58,58);
INSERT INTO `hive2` VALUES (2, 'xuzheng', 45,87,45);
INSERT INTO `hive2` VALUES (3, 'wangbaoqiang', 76,34,89);
行转列语句:
SELECT id, username , 'math' course ,math AS math FROM hive2
UNION SELECT id ,username,'computer' course ,computer as computer FROM hive2
UNION SELECT id ,username,'english' course ,english as english FROM hive2
ORDER BY id,username,course;
![Hive的考试练习_第6张图片](http://img.e-com-net.com/image/info8/700393e286fb4b078ee3b1c2c67b2bbf.jpg)
自定义数据:
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
2,23,44,55,2014-11-11,4
3,12,44,12,2015-1-11,5
1,22,33,34,2013-12-11,2
建表:
create table hive3
(user_id string,city_id int, cat_id string, visits_id int,dt string, hour int)
row format delimited fields terminated by ',';
load data local inpath '/home/potter/hive3.txt'
into table hive3;
第一小题:
PV
select city_id,dt,count(cat_id)
from hive3 where dt="2014-11-11"
group by city_id,dt;
结果:
23 2014-11-11 10
UV
select city_id ,dt,count(distinct user_id)
from hive3 where dt="2014-11-11"
group by city_id,dt;
结果:
23 2014-11-11 1
第二小题:
select city_id,dt,count(cat_id)
from hive3 where dt="2014-11-11" and city_id=23
group by city_id,dt;
结果:
23 2014-11-11 10
![Hive的考试练习_第7张图片](http://img.e-com-net.com/image/info8/7d3961e4cd8c4a42a1cc35db22b0a820.jpg)
建表:
create table hive4
(id int,name string, pid int, price int)
row format delimited fields terminated by ',';
load data local inpath '/home/potter/hive4.txt'
into table hive4;
查询语句:
select id, name, a.pid, a.price
from hive4 a
join (select pid, max(price) over (partition by pid)
as price from hive4) b on a.pid = b.pid
where a.price = b.price group by id, name, a.pid ,a.price;
![Hive的考试练习_第9张图片](http://img.e-com-net.com/image/info8/667a54967fa348a983b48f86438df823.jpg)
建表语句:
create table hive5
(id int,name string, age int, favor string)
row format delimited fields terminated by ':';
load data local inpath '/home/potter/hive5.txt'
into table hive5;
查询语句:
create table hive5_1 as
select a.id id, a.name name,a.age age, tv.favors
from hive5 a
lateral view explode(split(a.favor,",")) tv as favors;
select a.id, a.name, a.age, a.favors as favors
from hive5_1 a
join (select max(age) over (partition by favors) as age, favors
from hive5_1 group by age, favors) b on a.age = b.age and a.favors = b.favors
group by a.id, a.name, a.age, a.favors;