hive - 基于另一列的最大值的新列重复值
问题描述
我有一个表'mytable',其结果类似于下面
currenttime racetype raceid
2018-01-01 03:15:00 gold 22
2018-01-01 04:15:00 silver 22
2019-01-01 04:15:00 bronze 22
2017-01-02 11:44:00 platinum 22
我正在尝试根据最大当前时间创建另一列。它应该从最大当前时间中获取 racetype 的值,并为新列中的所有项目重复该条目,类似于下面
currenttime racetype raceid besttype
2018-01-01 03:15:00 gold 22 bronze
2018-01-01 04:15:00 silver 22 bronze
2019-01-01 04:15:00 bronze 22 bronze
2017-01-02 11:44:00 platinum 22 bronze
如果还有其他种族ID,它应该对那些前做同样的事情
currenttime racetype raceid besttype
2018-01-01 03:15:00 gold 22 bronze
2018-01-01 04:15:00 silver 22 bronze
2019-01-01 04:15:00 bronze 22 bronze
2017-01-02 11:44:00 platinum 22 bronze
2011-01-01 03:15:00 gold 09 silver
2022-01-01 04:15:00 silver 09 silver
2002-01-01 04:15:00 bronze 09 silver
目前我有一个查询
select mt.raceid, tt.racetype, MAX(tt.currenttime)
OVER (PARTITION by mt.raceid)
from mytable mt
join tabletwo tt on mt.id = tt.id
where mt.raceid = 22
此查询未输出预期的输出
raceid racetype col0
22 gold 2019-01-01 04:15:00
22 silver 2019-01-01 04:15:00
22 platinum 2019-01-01 04:15:00
22 bronze 2019-01-01 04:15:00
我怎样才能实现第二个和第三个示例中显示的上述预期结果?
解决方案
使用first_value解析函数:
select currenttime, racetype, raceid,
first_value(racetype) over(partition by raceid order by currenttime desc) as besttype
from mytable
或者last_value
:
select currenttime, racetype, raceid,
last_value(racetype) over(partition by raceid order by currenttime) as besttype
from mytable
推荐阅读
- asp.net - 多对多表的 Asp.net 核心 WebApi 端点
- opencv - 英特尔神经计算棒 2 是否提高了树莓派 4 上的 OpenCV2 性能
- kotlin - 如何在firestore android中获取经过身份验证的用户数据
- node.js - NodeJS 如何处理传入的请求
- node.js - 如何将一个用户帐户限制为仅两台设备
- c++ - 在基类上自动推断派生类上函数的返回类型
- python - python: groupby list of dataframes in to smaller lists of dataframes
- reactjs - Redux 状态和 useState 不同步
- laravel - 为什么 laravel 返回一个空数组来表示一个有很多关系?
- python - 将列表加入分隔符分隔的列表列表