Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order by count desc in each group in a hive?

Tags:

group-by

hql

hive

Here's the HQL:

select A, B, count(*) as cnt from test_table group by A, B order by cnt desc;

The sample output is as follows:

a1 | b1 | 5
a2 | b1 | 3
a1 | b2 | 2
a2 | b2 | 1

But what I want is to do the order by in each group of A, and the intended output is like:

a1 | b1 | 5
a1 | b2 | 2
a2 | b1 | 3
a2 | b2 | 1

Could anyone can give me some idea how to resolve this problem in just one HQL? Thanks a lot!

like image 966
Judking Avatar asked Dec 05 '13 03:12

Judking


People also ask

How do you sort by descending in Hive?

ORDER BY Ascending and Descending You can also specify ORDER BY <column names> ASC for ascending order and ORDER BY <column name> DESC for sorting the result in descending order or the specified column.

How do I select a count in Hive?

Simple Examples. In order to count the number of rows in a table: SELECT COUNT(*) FROM table2; Note that for versions of Hive which don't include HIVE-287, you'll need to use COUNT(1) in place of COUNT(*).

How does ORDER BY work in Hive?

Hive uses the columns in SORT BY to sort the rows before feeding the rows to a reducer. The sort order will be dependent on the column types. If the column is of numeric type, then the sort order is also in numeric order. If the column is of string type, then the sort order will be lexicographical order.


1 Answers

select A, B, count(*) as cnt 
from test_table 
group by A, B 
order by A, cnt desc;
like image 95
dimamah Avatar answered Jan 17 '23 12:01

dimamah