C/C++教程

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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!