Hive Grouping Sort, TopN

Source: Internet
Author: User
Tags ord

Hive Grouping Sort, TopN

Syntax format: row_number () over (partition by COL1 ORDER BY COL2 Desc) rank
Partition by: Hive-like build table, meaning of partition;
ORDER BY: Sort, default is ascending, plus desc descending;
Rank: Indicates an alias
Represents the sort based on COL2 within the grouping according to the COL1 grouping, and the value computed by this function represents the sequential number of each set of internally ordered (contiguous unique within the group)

--Grouping sort
--Ask for the maximum 3 days of a user's date
Select A.* from (
Select P_day,muuid,row_number () over (partition by Muuid ORDER by P_day) rank
From Test
Group by P_day,muuid) a
where A.rank <=3;
----Get the top 3 daily recharge numbers
SELECT * FROM (
Select P_day,muuid,c row_number over (partition by P_day ORDER BY p_day,c Desc) Ord
From
Select P_day,muuid,count (1) c from test where p_day> ' 2017-09-09 '
GROUP BY P_day,muuid
) T1
) T2
where Ord <= 3;

Hive Grouping Sort, TopN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.