hive 表达式不在按键分组中

发布于 2024-11-03 01:07:43 字数 501 浏览 3 评论 0原文

我在 HIVE 中创建了一个表。 它有以下列:

id bigint, rank bigint, date string

我想获得每月的平均(排名)。我可以使用这个命令。有用。

select a.lens_id, avg(a.rank)
from tableA a
group by a.lens_id, year(a.date_saved), month(a.date_saved); 

但是,我还想获取日期信息。我使用这个命令:

select a.lens_id, avg(a.rank), a.date_saved
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);

它抱怨:Expression Not In Group By Key

I create a table in HIVE.
It has the following columns:

id bigint, rank bigint, date string

I want to get avg(rank) per month. I can use this command. It works.

select a.lens_id, avg(a.rank)
from tableA a
group by a.lens_id, year(a.date_saved), month(a.date_saved); 

However, I also want to get date information. I use this command:

select a.lens_id, avg(a.rank), a.date_saved
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);

It complains: Expression Not In Group By Key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

打小就很酷 2024-11-10 01:07:43

完整的错误消息应采用以下格式:Expression Not In Group By Key [value]
[value] 将告诉您 Group By 中需要包含什么表达式。

只要看一下这两个查询,我就会说您需要将 a.date_saved 显式添加到 Group By 中。

The full error message should be in the format Expression Not In Group By Key [value].
The [value] will tell you what expression needs to be in the Group By.

Just looking at the two queries, I'd say that you need to add a.date_saved explicitly to the Group By.

请你别敷衍 2024-11-10 01:07:43

解决方法是将附加字段放入collect_set 中并返回该集合的第一个元素。例如

select a.lens_id, avg(a.rank), collect_set(a.date_saved)[0]
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);

A walk around is to put the additional field in a collect_set and return the first element of the set. For example

select a.lens_id, avg(a.rank), collect_set(a.date_saved)[0]
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
苦妄 2024-11-10 01:07:43

这是因为您的分组依据下有多个“date_saved”记录。您可以将这些“date_saved”记录转换为数组并输出它们。

This is because there is more than one ‘date_saved’ record under your group by. You can turn these ‘date_saved’ records into arrays and output them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文