选择HIVE中分区的最后一行

时间:2015-08-05 19:48:25

标签: sql hive hiveql

我有一张桌子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

我想选择每个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 

这会有用吗? (有环境问题所以无法测试自己)

1 个答案:

答案 0 :(得分:2)

只需使用子查询:

$('.refresh').click(function() {
    $('#table').bootstrapTable('refresh');
});

请注意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