建表:
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;
第二小题:
(2)
select name
from hive1 where score < 60 group by name;
先建表:
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;
自定义数据:
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
建表:
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;
建表语句:
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;