Hive - 获取没有空白列的最新记录
Hive - get latest record with no blank column
我有 2 个 table:
Table1:
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
My expected output is:
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,则取那个(样本emp_id: 2
- 如果最新的记录没有emp_log_id则回到之前更新的记录(sample emp_id: 1)
如何为此编写配置单元查询。
另一个 table 的数据如下:
Table 2:
emp_id emp_log_id
1 234
1 05
2 335
2 03
3 04
4 324
如何在这个table 2.
中实现相同的需求
请帮忙。
使用聚合函数 max()
即可。
select emp_id, max(emp_log_id) as emp_log_id, timestamp
from table1
group by emp_id, timestamp
我会推荐 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by emp_id
order by (emp_log_id is not null) desc, timestamp desc
) as seqnum
from t
) t
where seqnum = 1
我有 2 个 table: Table1:
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
My expected output is:
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,则取那个(样本emp_id: 2
- 如果最新的记录没有emp_log_id则回到之前更新的记录(sample emp_id: 1)
如何为此编写配置单元查询。
另一个 table 的数据如下: Table 2:
emp_id emp_log_id
1 234
1 05
2 335
2 03
3 04
4 324
如何在这个table 2.
中实现相同的需求请帮忙。
使用聚合函数 max()
即可。
select emp_id, max(emp_log_id) as emp_log_id, timestamp
from table1
group by emp_id, timestamp
我会推荐 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by emp_id
order by (emp_log_id is not null) desc, timestamp desc
) as seqnum
from t
) t
where seqnum = 1