一种巧妙的hive sql数据去重方法

在处理hive数据时,有时会遇到带重复数据的表
如下:

id info timestamp
1 a 1619577515
2 b 1619579995
1 c 1619577516

我们想根据id去重,只保留时间戳(timestamp)最新的数据。
这时就有一种巧妙的方法去重, sql如下:

select
	a.id,
	a.info,
	a.timestamp
from
(
	select
		*,
		row_number() over (partition by userid order by update_time desc) as row_number
	from
		table_name
) a
where
	row_number = 1;

去重后的结果为:

id info timestamp
2 b 1619579995
1 c 1619577516

你可能感兴趣的:(大数据,hive,sql,大数据)