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

▼魔方 西西 提交于 2019-12-14 00:19:03

问题


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!


回答1:


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



回答2:


Try this query:

If you want only order of A then:

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

If you want order of A and B then:

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

Hope this helps.




回答3:


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


来源:https://stackoverflow.com/questions/20391008/how-to-order-by-count-desc-in-each-group-in-a-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!