按 SQL/Athena 中的一组列获取最大值

Get max value by a group of column in SQL/Athena

我有以下列:

code  note
1      10
2      8
1      9
3      5
3      4

我怎样才能得到具有最大平均音符的代码?

我的输出应该是代码 1,因为 AVG 是 9,5(代码 1)、8(代码 2)、4,5(代码 3)

我在雅典娜的查询:

select  code from table group by code, avg(note) ORDER BY DESC limit 1

输出错误:

GROUP BY clause cannot contain aggregations, window functions or grouping operations

您的分组函数必须在您的 SELECT 列列表中,而不是在您的 GROUP BY 子句中。

试试这个:

select code, avg(note) as avg_of_note
from table 
group by code
order by avg_of_note desc
limit 1

此外:我不会亲自命名专栏 code。我可能担心它会成为某些 SQL 方言中的保留字。