如何计算 Hive 中值出现的次数

How to count number of value occurrences in Hive

我有一个 table,我试图根据值本身收集有关值在 table 中出现的次数的指标。下面是我的 table:

Table 1

id | type 
1,   law
1,   law
1,   law
2,   business
2,   business
2,   business
3,   science
4,   medicine

这是我正在使用的 SQl 查询:

select type, count(distinct type) my_count from table1 group by type;

我的输出是:

type    | my_count
science,   1
medicine,  1

排除其他记录。我希望我的输出基本上是:

type    | my_count
science,    1
medicine,   1
business,   3
law,        3

我知道这似乎是一项简单的任务,但我是 Hive 的新手SQl。任何想法或建议将不胜感激。

我想你只需要 count(*):

select type, count(*) as my_count
from table1
group by type;

这有点讽刺。通常人们正在尝试 count(),他们需要了解 count(distinct)

请注意,您的版本不应过滤掉任何 type 值。但是,计数总是 1.