sql - Hive - 获取没有空白列的最新记录
问题描述
我有 2 个表: 表 1:
emp_id emp_log_id timestamp
1 234 04 Oct 2019 23:10
1 05 Oct 2019 23:10
2 335 04 Oct 2019 23:10
2 03 Oct 2019 23:10
3 04 Oct 2019 23:10
4 324 04 Oct 2019 23:10
我的预期输出是:
emp_id emp_log_id timestamp
1 234 04 Oct 2019 23:10
2 335 04 Oct 2019 23:10
3 04 Oct 2019 23:10
4 324 04 Oct 2019 23:10
- 如果最新记录有emp_log_id,则取那个(sample emp_id: 2
- 如果最新记录没有 emp_log_id 则返回之前更新的记录(示例 emp_id: 1)
如何为此编写配置单元查询。
另一个表的数据如下: 表 2:
emp_id emp_log_id
1 234
1 05
2 335
2 03
3 04
4 324
如何实现此表 2 中的相同要求。
请帮忙。
解决方案
使用聚合函数max()
就可以了。
select emp_id, max(emp_log_id) as emp_log_id, timestamp
from table1
group by emp_id, timestamp
推荐阅读
- javascript - 使用 localStorage 显示总分 - Javascript
- c++ - Boost/Array.hpp:没有这样的文件或目录(在 Visual Studio 2019 上)
- terraform - 访问嵌套的 terraform 输出值
- html - 为什么我的子菜单在 html css 中不起作用
- reactjs - 使用自定义 Axios 和 redux-saga 处理错误
- r - 从 R 中的链接列表下载 PDF 时出错
- twitter-bootstrap - 将 bootstrap/scss/mixins 加载到 Vue scss 组件中
- javascript - RequireJS 优化文件返回未定义
- c# - 等待用户点击多个控件中的一个控件
- javascript - REST API 新用户未发布到 MongoDB