如何使用配置单元查询查找最常见的颜色?

hkmswyz6  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(378)

我有一张下表

name  red  black  white
US      1    1      1
UK      0    1      0
EU      1    1      0
AUS     0    0      0

现在,我需要找出哪种颜色是最常见的?我使用下面的查询得到了所有颜色的计数。

select colorName,count(name) as total
from
(select
case
when red == 1 then 'red'
when black == 1 then 'black'
end as colorName, name from countrytable)a
group by colorName;

这将产生以下输出:

red black white
2   3     1

但我想得到最常见的值,即“black”。我在 hive 里怎么做?

Desired output:
black
0g0grzrc

0g0grzrc1#

将另一个步骤添加到按计数降序排序/排序中,并将其限制为前1条记录。

select colorName,count(name) as total
from
    (select
       case
           when red == 1 then 'red' 
           when black == 1 then 'black'
       end as colorName, name from countrytable)a
group by colorName
ORDER BY count(name) DESC 
LIMIT 1;

相关问题