首页 > 解决方案 > 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
  1. 如果最新记录有emp_log_id,则取那个(sample emp_id: 2
  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 中的相同要求。

请帮忙。

标签: sqlhivehiveql

解决方案


使用聚合函数max()就可以了。

select emp_id, max(emp_log_id) as emp_log_id, timestamp 
from table1
group by emp_id, timestamp

推荐阅读