在Hive中我有一个看起来像这样的表:
id | tier
---------
1 | silver
2 | silver
3 | gold
4 | platinum
1 | gold
3 | platinum
Tier被命令像银,金,铂一样上升。我想从表中做一个SELECT DISTINCT,这样,如果一个id出现不止一次,那么具有最高层的行就是那个被选中的行。
所以决赛桌看起来应该是这样的
id | tier
---------
2 | silver
4 | platinum
1 | gold
3 | platinum
我该如何撰写这样的查询?谢谢!
答案 0 :(得分:0)
WITH CTE AS
(SELECT id , tier ,ROW_NUMBER() OVER (PARTITION BY tier ORDER BY id desc) from tbalename as rno)
SELECT id , tier from CTE where rno =1 ;
链接ROW_NUMBER() https://cwiki.apache.org/confluence/display/Hive/LanguageManual+WindowingAndAnalytics
链接CTE https://blogs.oracle.com/taylor22/entry/hive_0_11_may_15