数据库
首页 > 数据库> > 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;

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

执行结果:

 

标签:function,used,postgresql,gender,age,att,test01,avg,SELECT
来源: https://www.cnblogs.com/eartha-deng/p/15825462.html