如何从hive表中找到最大值及其引用名称?

时间:2016-04-06 23:58:45

标签: hadoop hive

我有一个蜂巢桌"航空公司"像这样:

name     airline
USA      American Airline
Nepal    Jet Airline
Dubai    Emirates
USA      SouthWestern
USA      Quatar
USA      Delta

现在,我想知道哪个国家的航空公司数量最多。 我正在使用嵌套子查询。

select max(tot)
from
(select name as countryName, count(airline) as tot
from airline
group by name) a

这给出了航空公司的最大数量,在这种情况下是4。

4

但我也需要国名。因此,所需的输出是:

USA 4

我们如何使用子查询来做到这一点?我没有使用子查询就完成了。我需要使用子查询执行。任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:2)

您可以使用 static int findMiddle(int[][] grid) { int l = grid.length; if (l%2 == 1) { return grid[l/2][l/2]; } else { return (grid[l/2-1][l-1]+grid[l/2][0])/2; };

row_number()

在关系的情况下,这给了一个任意国家。如果您想要全部,只需将select a.* from (select name as countryName, count(airline) as tot, row_number() over (order by count(airline) desc) as seqnum from airline group by name ) a where seqnum = 1; 更改为row_number()

答案 1 :(得分:0)

你可以做类似的事情:

select name, max(tot)
from
(select name as countryName, count(airline) as tot
from airline
group by name) a

答案 2 :(得分:0)

不需要嵌套。

select country, count(*) as tot
      from airline
      group by country
order by tot desc
limit 1

答案 3 :(得分:0)

非常简单,只需尝试下面的表单即可获得最大值: SELECT MAX(column_name)FROM table_name;

相关问题