举个例子:
    table name:makerar
cname  | wmname |          avg           
--------+-------------+------------------------  
canada | zoro   |     2.0000000000000000
spain  | luffy  | 1.00000000000000000000  
spain  | usopp  |     5.0000000000000000
 
执行语句:
  SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname; 
 
错误:
      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;
    
    
    
    首先这个错误的原因是因为: 
 

  Before SQL3 (1999), the selected fields must appear in the GROUP BY clause[*].Interestingly enough,

even though the spec sort of allows to select non-grouped fields, major engines seem to not really like it.

Oracle and SQLServer just don't allow this at all. Mysql used to allow it by default, but now since 5.7 the

administrator needs to enable this option (ONLY_FULL_GROUP_BY) manually in the server configuration

for this feature to be supported.

    
    翻译过来就主要是:在SQL3(1999)标准之前,select 的字段必须也放在group by 的语句里(因为当如未
在group的相同字段出现不同值时,数据库引擎便不知道刚显示什么了,如上例)。主要的数据库引擎都不允
许这样的操作(有selected field 不放在group by中),即使mysql在5.7版本后也需要打开一个选项才能使用。
 
    这种操作在mysql上运行的情况:it doesn't work "well" in mysql -- in fact, they actually warn you in the docs
that if you do it, and all the "hidden" columns (those not in the GROUP BY) aren't 1-to-1 with the GROUP BY 
columns, then the results are unpredictable in every other database you just plain can't do it, so i wouldn't call 
what mysql does "doing it well"。
    
    解决办法
        1.先在子查询里进行聚合运算(sum,max等),在通过join连接
        

 SELECT m.cname, m.wmname, t.mx FROM (      SELECT cname, MAX(avg)  AS mx
FROM makerar GROUP BY cname ) t
JOIN makerar m ON m.cname = t.cname AND t.mx = m.avg ;
 
cname  | wmname |          mx           
--------+--------+------------------------
 canada | zoro   |     2.0000000000000000  
 spain  | usopp  |     5.0000000000000000
        
        
        2.使用window functions
        
  SELECT cname, wmname, MAX(avg) OVER (PARTITION BY cname) AS mx FROM makerar; 
        
 cname  | wmname |          mx           
--------+--------+------------------------
 canada | zoro   |     2.0000000000000000  
 spain  | luffy  |     5.0000000000000000  
 spain  | usopp  |     5.0000000000000000
 
 
        要去掉mx重复的话:

 SELECT DISTINCT /* distinct here matters, because maybe there are  various tuples for the same max
value */ m.cname, m.wmname, t.avg AS mx FROM ( SELECT cname, wmname, avg, ROW_NUMBER()
OVER (PARTITION BY avg DESC) AS rn FROM makerar ) t JOIN makerar m ON m.cname = t.cname AND
m.wmname = t.wmname AND t.rn = 1 ;
 
cname  | wmname |          mx           
--------+--------+------------------------  
canada | zoro   |     2.0000000000000000  
spain  | usopp  |     5.0000000000000000
        
    3.使用 DISTINCT ON
    
     SELECT DISTINCT ON (cname)      cname, wmname, avg FROM      makerar  ORDER BY      cname, avg DESC ;

关于postgresql group by 报错的更多相关文章

  1. mysql 5.7.28 中GROUP BY报错问题 SELECT list is not in GROUP BY clause and contains no

    ----mysql 5.7.28 中GROUP BY报错问题 SELECT list is not in GROUP BY clause and contains no------ 解决方案: sel ...

  2. mysql group by 报错异常解决

    mysql报错及其解决方式 1.在使用group by 查询一张表的数据的时候:select date,time,max(delaytime) as delaytime,sum(delaynum) a ...

  3. mysql group by 报错 ,only_full_group_by 三种解决方案

    报错信息  Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'data ...

  4. PostgreSQL 修改列报错:cannot be cast automatically to type integer

    如果你直接使用可视化工具修改一个varchar字段为int类型的时候,可能会报错, 这里就需要自己去写一个语句去修改了 调整执行语句:ALTER TABLE table_name ALTER COLU ...

  5. Postgresql重安装报错The database cluster initialisation failed.

    之前安装过PostgreSQL-9.6.5,卸载后,重装PostgreSQL-9.1.3版本,报错. 清除注册表,删除postgres账户,清除垃圾后,再次安装仍然报错. 最后改变默认安装路径,神奇的 ...

  6. MySQL报错注入函数汇总及常用注入语句

    版权声明:本文转载自网络内容,下面附原创链接原创链接:https://blog.csdn.net/Auuuuuuuu/article/details/91415165 常用函数 字符串连接函数,将多个 ...

  7. CTF-sql-group by报错注入

    本文章主要涉及group by报错注入的原理讲解,如有错误,望指出.(附有目录,如需查看请点右下角) 一.下图为本次文章所使用到 user表,该表所在的数据库为 test 二.首先介绍一下本文章所使用 ...

  8. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  9. 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。

    PostgreSQL添加新的服务器连接时,报错:

随机推荐

  1. 文件比较与同步工具——FreeFileSync

    1. 基本介绍 FreeFileSync是一个用于文件同步的免费开源程序.FreeFileSync通过比较其内容,日期或文件大小上的一个或多个文件夹,然后根据用户定义的设置同步内容.除了支持本地文件系 ...

  2. Appnium-API-Status

    Status Java:// TODO Python:selenium.webdriver.common.utils.is_url_connectable(port) Description Retu ...

  3. poj 3268 Silver Cow Party(最短路dijkstra)

    描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

  4. .net core读取json配置文件

    一.新建.net core控制台程序 二.通过Nuget引入 Microsoft.Extensions.Configuration和microsoft.extensions.configuration ...

  5. displaytag如何实现获取到每行的id字段的值。

    1.displaytag如何实现获取到每行的id字段的值. 使用封装好的框架,有时候,对于一个知识点不熟悉,可能会浪费你大把的时间,我使用displaytag主要是使用它的分页技术,但是客户提出的需求 ...

  6. git20181122

    git 在线编辑器 http://www.mdeditor.com/git add commit diff log status 代码撤消 # git https://github.com/gyz41 ...

  7. 几道比较有意思的js面试题

    1.[] ? !![] : ![];输出结果是什么? 1 2 3 4 5 let val = [] ? !![] : ![]; console.log(val);  //true: //之前的错误解释 ...

  8. About Why Inline Member Function Should Defined in The Header File

    About why inline member function should defined in the header file. It is legal to specify inline on ...

  9. 「LibreOJ Round #9」CommonAnts 的调和数

    题解: 对于subtask3:可以把相同的归在一起就是$nlogn$的了 对于subtask4: 可以使用高维前缀和的技术,具体的就是把每个质因数看作一维空间 那么时间复杂度是$\sum \limit ...

  10. SQL Server SubString和charindex的用法

    语法 SUBSTRING ( expression , start , length ) 参数 expression 是字符串.二进制字符串.text.image.列或包含列的表达式.不要使用包含聚合 ...