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 ...
随机推荐
- error LNK2001: unresolved external symbol __imp___rt_sdiv
这个问题搞了我 5 天(包括双休日), 我一定要记录下来. 问题描述 用 Visual Studio 2008 编译 WinCE 7 平台的应用程序,编译没问题,链接时出现了一堆 Link error ...
- HOSt ip is not allowed to connect to this MySql server, MYSQL添加远程用户或允许远程访问三种方法
HOSt ip is not allowed to connect to this MySql server 报错:1130-host ... is not allowed to connect to ...
- 前端开发之HTML
前端 编程主要就是三部分:使用数据,存储数据和处理数据. 什么是前端: 前端就是使用数据的过程,通过规定的格式将服务端的数据在浏览器上更好的展示给用户. 前端的工具: HTML CSS 和 JavaS ...
- CSS 实现单行及多行文字省略
单行文字省略 很多时候不确定字数限制,但换行可能影响整体设计,这个时候常用就是文字省略加全文字提示了 .dom{ text-overflow: ellipsis; overflow: hidden; ...
- 【9101】求n!的值
Time Limit: 10 second Memory Limit: 2 MB 问题描述 用高精度的方法,求n!的精确值(n的值以一般整数输入). Input 文件输入仅一行,输入n. Output ...
- js基础——基本包装类型
1.基本包装类型String var bz = new String("Li.Linda"); //引用类型(object) bz.name= bz.subst ...
- js基础——function类型
1.函数声明方式 1)普通声明方式 function box(num1,num2){ return num1 + num2; } 2)使用变量初始化函数 var box = funct ...
- 2018-8-10-win10-uwp-商业游戏-1.2.1
title author date CreateTime categories win10 uwp 商业游戏 1.2.1 lindexi 2018-08-10 19:16:50 +0800 2018- ...
- CSS3侧栏滑出简单实现
使用css3 的 animation 属性实现的点击滑出侧栏 <!DOCTYPE html> <html lang="en"> <head> & ...
- ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(7)
今天将使用Simplemembership进行权限控制 我们使用mvc的AuthorizeAttribute来实现对Controller and Action权限控制 看如下标为红色的代码片段: // ...