简体   繁体   English

在 SQL/Athena 中通过一组列获取最大值

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

I have the following columns:我有以下列:

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

How can I get the code that has the max average note?如何获得具有最大平均音符的代码?

My output should be code 1 since the AVGs are 9,5 (code 1), 8 (code 2), 4,5 (code 3)我的 output 应该是代码 1,因为 AVG 是 9,5(代码 1)、8(代码 2)、4,5(代码 3)

My query in athena:我在雅典娜的查询:

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

The output error: output 错误:

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

Your grouping function must be in your SELECT list of column, not in your GROUP BY clause.您的分组 function 必须在您的 SELECT 列列表中,而不是在您的 GROUP BY 子句中。

Try this instead:试试这个:

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

Also: I wouldn't personally name a column code .另外:我不会亲自命名列code I could be concerned it would be a reserved word in some SQL dialects.我可能担心它会成为某些 SQL 方言中的保留字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM