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 ...
随机推荐
- BraveOS正式版发布,希望大家下载使用
废话不多说,直接贴图才是王道 这里是DOS系统+默认官方(Platform系统) 下载地址:http://pan.baidu.com/s/1eQINwx8 (引导进Platform系统后,默认管理员帐 ...
- 【u128】又一个数字游戏
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明拿出了一个素数集合,{2, 3, 5, 7, 11, 13, -, 127, -},他发现,从小到 ...
- python基础四之列表
列表详解 列表的增删改查! 增加 li = ['zxc', 'is', 'a'] # append 在列表结尾整体添加 修改列表,但是没有返回值 li.append('boy') print(li) ...
- 分布式全局唯一ID
方案一.UUID UUID的方式能生成一串唯一随机32位长度数据,它是无序的一串数据,按照开放软件基金会(OSF)制定的标准计算,UUID的生成用到了以太网卡地址.纳秒级时间.芯片ID码和许多可能的数 ...
- 2018-8-10-win10-uwp-x_Bind-无法获得资源
title author date CreateTime categories win10 uwp x:Bind 无法获得资源 lindexi 2018-08-10 19:17:19 +0800 20 ...
- java编程规范大全
JAVA编程规范大全 命名规范 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失.(这些规范并不是一定要绝对遵守,但是一定要让程序有良好的可读性 ...
- SpringBoot的四种定时任务
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 使用这种方式可以让你的程序按照某一个频度执行 ...
- CF1208
CF1208 打的话貌似能够涨分的样子? A 水题 B 枚举左端点,看看右端点的最近位置 开一个类似于桶的东西维护一下上一次出现位置 左端点左边就删掉,否则就要将上一次出现的位置删掉 时间复杂度\(O ...
- swiper 使用参考 禁止手动滑动 监听事件
最外层容器加类名 swiper-no-swiping 监听切换事件 onTransitionEnd: function(swiper){ console.log('过渡结束'); }
- attr(),prop()二者区别和最佳实践
attr(),prop()二者区别和最佳实践 最近使用到attr()来获取自定义属性值,我印象中是有一个方法可以获取到自定义属性值,进而我又想到了另一个方法prop(). 查看了手册发现并没有对二者 ...