Hadoop Hive MAX提供多个结果

时间:2018-12-15 15:53:48

标签: hadoop hive bigdata hiveql

我正在尝试从选择2个标签srcip和max的计数中获取最大值,但是每次包含srcip时,我都必须在末尾使用group by srcip并给出结果,即使在那里也没有最大消耗。

当我这样编写查询时,它为我提供了正确的最大值,但我也想选择srcip。

Select max(count1) as maximum 
    from (SELECT srcip,count(srcip) as count1 from data group by srcip)t;

但是当我在选择中包含srcip时,由于没有max函数,我得到了结果

Select srcip,max(count1) as maximum 
from (SELECT srcip,count(srcip) as count1 from data group by srcip)t 
group by srcip;

我希望从中得到一个结果,但是得到多个结果。

有人有什么主意吗?

3 个答案:

答案 0 :(得分:0)

您可以对ORDER BY count DESC进行LIMIT 1来获取计数为MAX的股票。

SELECT srcip, count(srcip) as count1 
  from data group by srcip
ORDER BY count1 DESC LIMIT 1

答案 1 :(得分:0)

让我们考虑一下您有一个这样的数据。

表格

enter image description here

让我们看看您在执行以下查询时发生了什么,数据发生了什么。

查询

SELECT srcip,count(srcip) as count1 from data group by srcip

输出:table1

enter image description here

现在让我们看看在上面的表上运行外部查询会发生什么情况。

Select srcip,max(count1) as maximum from table1 group by srcip

相同的输出

原因是您的查询说要从每个srcip组中选择srcip和最大计数。我们有3组,所以3行。

enter image description here

答案 2 :(得分:0)

下面的查询返回精确的一行,该行具有最大计数和关联的脚本。这是基于预期结果的查询;您宁愿更多地研究sql和更早的注释,然后继续进行分析查询。

有些人可能会说,有一种更好的方法可以针对您的预期结果优化此查询,但这应该使您有动力去更深入地研究Hive分析查询。

select scrip, count1 as maximum from (select srcip, count(scrip) over (PARTITION by scrip) as count1, row_number() over (ORDER by scrip desc) as row_num from data) q1 having row_num = 1;