【每日一练:SQL】Hive SQL求每一年最大气温的日期+温度
Hive SQL求每一年最大气温的日期+温度
HIVE SQL题:
求每一年最大气温的日期+温度,具体数据如下:
2014010114
2014010216
2014020317
2014010410
2014030506
2012010609
2012010732
2012010812
2012010919
2012011023
2001020116
2001010212
2001050310
2001010411
2001010529
2013010619
2013010722
2013010812
2013010929
2013071023
2008010105
2008010216
2008040337
2008010414
2008040516
2007010619
2007080712
2007010812
2007010999
2007011099
2010040114
2010010216
2010010317
2010070410
2010010506
2015020649
2015010722
2015010812
2015050999
2015011099
数据解释
2014010114表示在2014年01月01日的气温为14度
解答:
以下是使用Oracle经过验证,但没有在hive环境验证!!!
需要注意是每年出现最高气温的日期和温度,每一年有可能在不同日期出现最高温度。
--创建表
create table tb_weather(t_date int);
--插入数据
insert into tb_weather values(2014010114);
insert into tb_weather values(2014010216);
insert into tb_weather values(2014020317);
insert into tb_weather values(2014010410);
insert into tb_weather values(2014030506);
insert into tb_weather values(2012010609);
insert into tb_weather values(2012010732);
insert into tb_weather values(2012010812);
insert into tb_weather values(2012010919);
insert into tb_weather values(2012011023);
insert into tb_weather values(2001020116);
insert into tb_weather values(2001010212);
insert into tb_weather values(2001050310);
insert into tb_weather values(2001010411);
insert into tb_weather values(2001010529);
insert into tb_weather values(2013010619);
insert into tb_weather values(2013010722);
insert into tb_weather values(2013010812);
insert into tb_weather values(2013010929);
insert into tb_weather values(2013071023);
insert into tb_weather values(2008010105);
insert into tb_weather values(2008010216);
insert into tb_weather values(2008040337);
insert into tb_weather values(2008010414);
insert into tb_weather values(2008040516);
insert into tb_weather values(2007010619);
insert into tb_weather values(2007080712);
insert into tb_weather values(2007010812);
insert into tb_weather values(2007010999);
insert into tb_weather values(2007011099);
insert into tb_weather values(2010040114);
insert into tb_weather values(2010010216);
insert into tb_weather values(2010010317);
insert into tb_weather values(2010070410);
insert into tb_weather values(2010010506);
insert into tb_weather values(2015020649);
insert into tb_weather values(2015010722);
insert into tb_weather values(2015010812);
insert into tb_weather values(2015050999);
insert into tb_weather values(2015011099);
--提交
commit;
--创建第一个临时表,将年、月、日、温度分开
create table tb_weather_data
as
select substr(t_date,1,4) year, substr(t_date,5,2) mouth, substr(t_date,7,2) day, substr(t_date,9,2) tempfrom tb_weather;--查询新建的表
select * from tb_weather_data;YEAR MOUTH DAY TEMP
2014 01 01 14
2014 01 02 16
2014 02 03 17
2014 01 04 10
2014 03 05 06
2012 01 06 09
2012 01 07 32
2012 01 08 12
2012 01 09 19
2012 01 10 23
2001 02 01 16
2001 01 02 12
2001 05 03 10
2001 01 04 11
2001 01 05 29
2013 01 06 19
2013 01 07 22
2013 01 08 12
2013 01 09 29
2013 07 10 23
2008 01 01 05
2008 01 02 16
2008 04 03 37
2008 01 04 14
2008 04 05 16
2007 01 06 19
2007 08 07 12
2007 01 08 12
2007 01 09 99
2007 01 10 99
2010 04 01 14
2010 01 02 16
2010 01 03 17
2010 07 04 10
2010 01 05 06
2015 02 06 49
2015 01 07 22
2015 01 08 12
2015 05 09 99
2015 01 10 99
注意:substr开始的0和1也是从第一个字符算起,但实际是从1开始算!!!
--创建第二个临时表:求出每年最高的气温
create table tb_weather_year_temp
as
select year --年份, max(temp) max_temp --最高温度from tb_weather_datagroup by year;--查询新建的表tb_weather_year_temp
select * from tb_weather_year_temp;
YEAR MAX_TEMP
2001 29
2010 17
2014 17
2015 99
2013 29
2008 37
2007 99
2012 32
--每一年出现过的最大气温的日期+温度
select a.year || '年' || a.mouth || '月' || a.day || '日' as "Time", b.max_temp as "当年最高温度"from tb_weather_data ainner join tb_weather_year_temp bon a.year = b.yearand b.max_temp = a.temporder by a.year desc;结果:
Time 当年最高温度
2015年05月09日 99
2015年01月10日 99
2014年02月03日 17
2013年01月09日 29
2012年01月07日 32
2010年01月03日 17
2008年04月03日 37
2007年01月09日 99
2007年01月10日 99
2001年01月05日 29
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
