hive-top n?

58wvjzkj  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(510)

样本表

value    measurement1  measurement2
-------|-------------|-----------
value1       1         **2**
value2   **3**     **3**
value3   **2**         1

然后找到前2个最高值,我想得到下面的输出:

top 2 by measurement1 top 2 by measurement2 
---------------------|----------------------
      value2                  value2    
      value3                  value1
0yg35tkg

0yg35tkg1#

你可以使用 row_number() 以及 join :

select s1.value1 as col1,
       s2.value2 as col2
from (select s.*,
             row_number() over (order by value1) as seqnum
      from sample s
     ) s1 join
     (select s.*,
             row_number() over (order by value2) as seqnum
      from sample s
     ) s2
     on s1.seqnum = s2.seqnum
where s1.seqnum <= 2
order by s1.seqnum;

相关问题