选择 HIVE 分区中的最后一行

Selecting the the last row in a partition in HIVE

我有一个 table t1:

c1 | c2 | c3| c4
1    1    1   A
1    1    2   B
1    1    3   C
1    1    4   D
1    1    4   E
1    1    4   F
2    2    1   A
2    2    2   A
2    2    3   A

我想select每个c1、c2对的最后一行。所以在这种情况下 (1,1,4,F) 和 (2,2,3,A)。我的想法是做这样的事情:

create table t2 as
select *, row_number() over (partition by c1, c2 order by c3) as rank
from t1

create table t3 as
select a.c1, a.c2, a.c3, a.c4
from t2 a
inner join
(select c1, c2, max(rank) as maxrank
 from t2
 group by c1, c2
 )
on a.c1=b.c1 and a.c2=b.c1
where a.rank=b.maxrank 

这行得通吗? (有环境问题无法自己测试)

只需使用子查询:

select t1.*
from (select t1.*, row_number() over (partition by c1, c2 order by c3 desc) as rank
      from t1 
     ) t1
where rank = 1;

请注意 desc 用于 order by