前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive案例03-最高气温

Hive案例03-最高气温

作者头像
CoderJed
发布2018-09-13 10:50:00
4170
发布2018-09-13 10:50:00
举报
文章被收录于专栏:Jed的技术阶梯Jed的技术阶梯

1. 题目要求

现有hive表temp,其中只有一个字段(temp_record string),每一行代表某一天的气温,比如,2014010114代表,2014年1月1日的气温为14度,表中数据如下:

代码语言:javascript
复制
hive> select * from temp;

2014010114
2014010216
2014010317
2014010410
2014010506
2012010609
2012010732
2012010812
2012010919
2012011023
2001010116
2001010212
2001010310
2001010411
2001010529
2013010619
2013010722
2013010812
2013010929
2013011023
2008010105
2008010216
2008010337
2008010414
2008010516
2007010619
2007010712
2007010812
2007010999
2007011023
2010010114
2010010216
2010010317
2010010410
2010010506
2015010649
2015010722
2015010812
2015010999
2015011023

要求:用hive求出每一年的最大气温那条记录 注意:数据格式不能改变,例如求出来2015年的最大气温那条记录为2015010999

2. 参考答案

代码语言:javascript
复制
--(1)
create table temp_column as
SELECT substring(temp_record, 1, 4) year, substring(temp_record, 5, 4) day, substring(temp_record, 9) temp 
FROM temp;

/* temp_column表中数据:
2014    0101    14
2014    0102    16
2014    0103    17
2014    0104    10
......
*/

--(2)
create table temp_index as
select year, day, temp, 
row_number() over(distribute by year sort by temp desc) as index 
from temp_column;

/*  temp_index表中数据:
2001    0105    29  1
2001    0101    16  2
2001    0102    12  3
2001    0104    11  4
2001    0103    10  5
......
*/

--(3)
select concat(year, day, temp) from temp_index where index = 1;

/* 最终结果:
2001010529
2007010999
2008010337
2010010317
2012010732
2013010929
2014010317
2015010999
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.12.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目要求
  • 2. 参考答案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档