我有一张桌子,看起来像这个呼叫者“ makerar”
cname | wmname | avg
--------+-------------+------------------------
canada | zoro | 2.0000000000000000
spain | luffy | 1.00000000000000000000
spain | usopp | 5.0000000000000000
我想为每个cname选择最大平均值。
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname;
但我会出错
ERROR: column "makerar.wmname" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname;
所以我这样做
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname, wmname;
但是,这不会给出预期的结果,并且下面显示了错误的输出。
cname | wmname | max
--------+--------+------------------------
canada | zoro | 2.0000000000000000
spain | luffy | 1.00000000000000000000
spain | usopp | 5.0000000000000000
实际结果应为
cname | wmname | max
--------+--------+------------------------
canada | zoro | 2.0000000000000000
spain | usopp | 5.0000000000000000
我该如何解决这个问题?
注意:此表是根据上一个操作创建的VIEW。
wmname="usopp"
期望而不是例如wmname="luffy"
?