Oracle:使用avg函数选择多个列

时间:2019-07-01 09:50:01

标签: oracle oracle10g sqlplus

我正在尝试按玩家姓名逐行显示每个玩家的平均值 我遍历了oracle文档和其他人在堆栈溢出时提出的先前问题。我只遇到过如何通过记录ID选择平均值组以及如何使用子查询选择平均值的方法

例如, 我发现的是通过

来获取每一行的平均值
select round(avg(g1+g2+g3+g4)) as "Average Score" from ch_user group by playerid; 

请注意,我没有显示图片中显示的PlayerID。 enter image description here

如果我选择玩家和avg,它将给我ORA-01427。
我尝试过

select player, (select round(avg(g1+g2+g3+g4)) from ch_user group by playerid) as "Average Score" from ch_user;

但是它提示错误ORA-01427。
单行子查询返回多行。

提前谢谢

已更新

我已经找到了解决问题的方法,并且能够根据@Boneist答案将其实现到我的查询中。

但是,我发现查询很长,是否有可能简化查询?

我的查询是

select first_name || ' ' || last_name as Player,game_1 as G1 , game_2 as g2 ,
game_3 as g3, game_4 as G4,total_score as "Total Tournament Score",
round(avg(game_1+game_2+game_3+game_4)) as "Average Score" from ch_user
group by playerid, first_name,last_name,game_1,game_2,game_3,game_4,total_score;

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为您已经超出了预期……您要做的就是将player添加到第一个查询的选择列表中,并将player添加到组中,方法是:

select player,
       round(avg(g1 + g2 + g3 + g4)) as "Average Score"
from   ch_user
group by playerid, player;

答案 1 :(得分:1)

我认为您想按player_id对数据进行分组,但要显示玩家名称和平均值。检查是否有帮助。

select player_name,round(avg(g1+g2+g3+g4)) "Average"
from ch_user
group by player_id,player_name;