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 ...
随机推荐
- hdu 1556 Color the ball(区间更新,单点求值)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3181 Dollar Dayz(完全背包)
Dollar Dayz Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5419 Accepted: 2054 Descr ...
- Python--day62--编辑出版社功能
1,Django项目主要用到的文件
- cdmc2016数据挖掘竞赛题目Android Malware Classification
http://www.csmining.org/cdmc2016/ Data Mining Tasks Description Task 1: 2016 e-News categorisation F ...
- Mysql怎样控制replace替换的次数?
我想把“ABC是ABC”替换成“123是ABC”,也就是找出第一个ABC替换成123,MYSQL命令应该怎么写? UPDATE data SET body=REPLACE(body, 'ABC', ' ...
- [转]在ASP.NET WebAPI 中使用缓存【Redis】
初步看了下CacheCow与OutputCache,感觉还是CacheOutput比较符合自己的要求,使用也很简单 PM>Install-Package Strathweb.CacheOutpu ...
- python类中的双下划线方法
__getitem__,__setitem__和__delitem__ 实现了对象属性的字典化操作. class Person: def __init__(self, name, age, hobby ...
- vue-lazyload: 想弃坑,但没有找到合适的替代品
vue-lazyload,相信在vue项目中大家都有用到过它,同时也遇到过大大小小的坑.笔者也遇到过这样一个bug,在一个图片列表页面中,总有一定的概率图片的状态为load,导致图片一直加载中...这 ...
- element el-table 添加分页连接的序号,清除sortable排序
先看代码: <el-table :data="tableData" style="width: 100%" stripe size="mediu ...
- linux ioctl 系统调用预定义的命令
尽管 ioctl 系统调用最常用来作用于设备, 内核能识别几个命令. 注意这些命令, 当用 到你的设备时, 在你自己的文件操作被调用之前被解码. 因此, 如果你选择相同的号给一 个你的 ioctl 命 ...