postgresql--column must appear in the group by clause or be used in an aggregate function

我想得到男女当中大于各自性别平均年龄的人

原表:

postgresql--column must appear in the group by clause or be used in an aggregate function

在gauss200下执行以下语句:

SELECT stname,age,gender,AVG(age) FROM att_test01 GROUP BY gender HAVING age > AVG(age); 

 报错:column att_test01.stname must appear in the group by clause or be used in an aggregate function

gauss200是基于开源的postgres-XC开发的分布式关系型数据库系统,这是postgres常见的聚合问题。

解决方法如下:

SELECT b.stname,b.gender,b.age,a.avg FROM (SELECT gender,AVG(age) AS avg FROM att_test01 GROUP BY gender) a LEFT JOIN att_test01 b ON a.gender = b.gender AND b.age > a.avg;

将聚合放到子查询当中,然后与原表进行联合查询。

执行结果:

postgresql--column must appear in the group by clause or be used in an aggregate function

 

上一篇:前端开发中一些常用的sass混入


下一篇:【BZOJ3524/2223】[Poi2014]Couriers 主席树