Handling of expression not in group by key [value] reported by hive on October 12, 2020

hive Expression Not In Group By Key>我在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

回答:
完整错误消息的格式应为Expression Not in Group By Key [value]。
[value]会告诉你需要在的组中是什么表达式。仅看这两个查询,我认为您需要显式地将a.date_saved添加到Group By中。

问题:是的。添加了a.date_saved之后,它就工作了。然而,它并没有做到我想要的。我想要一个月的平均薪资。现在它不做平均。它只显示了通过a.date_saved添加组之后的所有记录。
回答:
您不能选择一个列,并且不按照该列进行分组。如果您想要显示a.date_saved,您需要根据它进行分组。你可以显示year(a.date_saved)和month(a.date_saved),因为它们在组中,但不是100%

解决方案二:
走动是将额外的字段在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);

– customer unit price (including reject), in HQL, in select… Group by… To display a field in>, you must group by that field, otherwise do not select that field

select user_id,sum(order_money)/count(order_money) from us_order;

error:
modified answer:

select user_id,sum(order_money)/count(order_money) from us_order group by user_id;

select collect_set(user_id)[0],sum(order_money)/count(order_money) from us_order;


Read More: