must appear in the GROUP BY clause or be used in an aggregate function
今天在分组统计的时候pgsql报错 must appear in the GROUP BY clause or be used in an aggregate function
,在mysql里面是可以的,但是pgsql报错,我去stackoverflow查询了一下,发现有人遇到过和我一样的问题,这是pgsql一个常见的聚合问题,在SQL3标准以前,选择显示的字段必须出现在在 GROUP BY
中。下面我把问题描述一下:
有一张表叫 makerar
,表中记录如下:
cname | wmname | avg |
---|---|---|
canada | zoro | 2.00 |
spain | luffy | 1.00 |
spain | usopp | 5.00 |
我想要查询每个 cname
的最大 avg
,按照mysql的写法是
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname;
在pgsql中报错
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;
意思是 wmname
字段必须在 GROUP BY
中出现或者被用于聚合函数
于是我按照错误提示,把 wmname
字段加在 GROUP BY
后面,即
SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname, wmname;
得到的结果是
cname | wmname | avg |
---|---|---|
canada | zoro | 2.00 |
spain | luffy | 1.00 |
spain | usopp | 5.00 |
而我期望得到的结果是
cname | wmname | avg |
---|---|---|
canada | zoro | 2.00 |
spain | usopp | 5.00 |
解决方案有两种,但是我只看懂了一种,于是把这一种记录一下
大体思路是在子查询中完成聚合,然后关联包含你想显示字段的表(这里是makerar自身)获取字段(这里是wmname),所以sql就变成了下面这个样子
SELECT
t.cname,
m.wmname,
t.max
FROM
(SELECT
cname,
MAX(avg) AS max
FROM makerar
GROUP BY cname) t
LEFT JOIN makerar m ON t.cname = m.cname AND t.max = m.avg;
参考链接
must appear in the GROUP BY clause or be used in an aggregate function的更多相关文章
- 【理解】column must appear in the GROUP BY clause or be used in an aggregate function
column "ms.xxx_time" must appear in the GROUP BY clause or be used in an aggregate functio ...
- [mysql] Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'loser.tank_admin.login_ip' which is not functionally dependent on columns in GROUP BY clause; this is
执行SQL: SELECT login_name,login_ip,sex FROM tank_admin GROUP BY login_name ; 时抛出异常. Expression #2 of ...
- InfluxDB:cannot use field in group by clause
最近在使用InfluxDB时,发现一个很奇怪的问题,一个本来正常的功能,做了一次改动后,就不能正常显示了. 一.查询语句 SELECT MEMORY FROM "ACM_PROCESS_MO ...
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause 的问题 MySQL
问题:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregate ...
- SELECT list is not in GROUP BY clause and contains nonaggregated column
报错如下: Expression # of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘sss.m ...
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
安装了mysql5.7.19后,执行语句中只要含有group by 就会报这个错 [Err] 1055 - Expression #1 of ORDER BY clause is not in GRO ...
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
安装了mysql5.7,用group by 查询时抛出如下异常: Expression # of SELECT list is not in GROUP BY clause and contains ...
- 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai
之前一直使用的mysql5,突然换成8之后,有许多地方不一样,今天就碰到一个. 在使用sql语句创建表时,报错: 1055 - Expression #1 of ORDER BY clause is ...
- MYSQL---Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column '
--数据库中插入数据或执行sql语句时一直报下面这个错误: Expression # of ORDER BY clause is not in GROUP BY clause and contains ...
随机推荐
- Python--day23--面向对象思想求正方形面积
- springmvc web.xml和application.xml配置详情(附:完整版pom.xml)
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...
- 浅谈集合框架四——集合扩展:集合循环输出方式及list输出方式的效率对比
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- python基础七之集合
集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. 增加 set1 = {'zxc', 'zxf'} set1.add('zxv') # 无序添加 set1.update('zx ...
- H3C 不适当的VLAN间路由方式
- P1023 活动安排
题目描述 某个人可以在n个活动中选择一些出来参加.每个活动都有起止时间.而且每个时间段只能参加一个活动.问,这个人最多能加参加几个活动. 可以在活动结束时,立即开始新的活动. 输入格式 第一行是一个整 ...
- JSON怎样添加注释
今天在写一个程序的时候发现了一个问题,在json文件中添加注释之后,程序就出现bug了 于是,去搜了一下这个问题的相关解释,在这里和大家分享一下: JSON为什么不能添加注释? 这位外国友人给出的解释 ...
- JPA查询之Specification以及HQL、SQL查询
1.Specification //查询条件List List<Predicate> predicateList = new ArrayList<Predicate>(); S ...
- 2018-8-10-win10-uwp-获得缩略图
title author date CreateTime categories win10 uwp 获得缩略图 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...
- Linux 内核USB 驱动
通用串行总线(USB)是一个在主机和许多外设之间的连接. 最初它被创建来替代许多慢速和不同的总线- 并口, 串口, 和键盘连接--有一个单个的所有设备都可以连接的总线类型.[45] USB 已经成长超 ...