首页 > 解决方案 > 如何在 hive 中实现无分组的计数(不同)

问题描述

我想在 hive 中使用 group by 来查找计数(不同的列名)。我的输入是:

name  id
a      2
a      3
a      4
b      1
c      4
c      4
d      7
d      9

我的预期输出是

name   count
a       3
b       1
c       1
d       2

有人可以告诉我如何在不使用 group by 的情况下实现这一目标。请帮忙

标签: sqlhive

解决方案


group by没有显式的规范解决方案select与窗口函数不同:

select distinct name, count(distinct id) over (partition by name)
from t;

就您而言,我强烈推荐以下group by版本:

select name, count(distinct id)
from t
group by name;

推荐阅读