没有聚合函数的 GROUP BY

GROUP BY without aggregate function(没有聚合函数的 GROUP BY)
本文介绍了没有聚合函数的 GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在没有聚合函数的情况下理解 GROUP BY (oracle dbms 的新手).
它是如何运作的?
这是我尝试过的.

我将在其上运行 SQL 的 EMP 表.

SELECT ename , sal发件人按名称分组

SELECT ename , sal发件人按名称分组;

<块引用>

结果

ORA-00979:不是 GROUP BY 表达式
00979. 00000 - 不是 GROUP BY 表达式"
*原因:
*动作:
行错误:397 列:16

SELECT ename , sal发件人按萨尔分组;

<块引用>

结果

ORA-00979:不是 GROUP BY 表达式
00979. 00000 - 不是 GROUP BY 表达式"
*原因:
*操作:错误行:411 列:8

SELECT empno , ename , sal发件人GROUP BY 萨尔 , 名字;

<块引用>

结果

ORA-00979:不是 GROUP BY 表达式
00979. 00000 - 不是 GROUP BY 表达式"
*原因:
*操作:错误行:425 列:8

SELECT empno , ename , sal发件人GROUP BY empno , ename , sal;

所以,基本上列数必须等于 GROUP BY 子句中的列数,但我仍然不明白为什么或发生了什么.

解决方案

这就是 GROUP BY 的工作原理.它需要几行并将它们变成一行.因此,它必须知道如何处理某些列(字段)具有不同值的所有组合行.这就是为什么您对要 SELECT 的每个字段都有两个选项:要么将其包含在 GROUP BY 子句中,要么在聚合函数中使用它,以便系统知道您希望如何组合该字段.

例如,假设您有这张表:

名称 |订单号------------------约翰 |1约翰 |2

如果你说 GROUP BY Name,它怎么知道在结果中显示哪个 OrderNumber?所以你要么在 group by 中包含 OrderNumber,这将导致这两行.或者,您使用聚合函数来展示如何处理 OrderNumber.例如MAX(OrderNumber),表示结果为John |2SUM(OrderNumber) 表示结果是 John |3.

I am trying to understand GROUP BY (new to oracle dbms) without aggregate function.
How does it operate?
Here is what i have tried.

EMP table on which i will run my SQL.

SELECT ename , sal
FROM emp
GROUP BY ename , sal

SELECT ename , sal  
FROM emp  
GROUP BY ename;  

Result

ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 397 Column: 16

SELECT ename , sal  
FROM emp  
GROUP BY sal;  

Result

ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action: Error at Line: 411 Column: 8

SELECT empno , ename , sal  
FROM emp  
GROUP BY sal , ename;  

Result

ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action: Error at Line: 425 Column: 8

SELECT empno , ename , sal  
FROM emp  
GROUP BY empno , ename , sal;  

So, basically the number of columns have to be equal to the number of columns in the GROUP BY clause, but i still do not understand why or what is going on.

解决方案

That's how GROUP BY works. It takes several rows and turns them into one row. Because of this, it has to know what to do with all the combined rows where there have different values for some columns (fields). This is why you have two options for every field you want to SELECT : Either include it in the GROUP BY clause, or use it in an aggregate function so the system knows how you want to combine the field.

For example, let's say you have this table:

Name | OrderNumber
------------------
John | 1
John | 2

If you say GROUP BY Name, how will it know which OrderNumber to show in the result? So you either include OrderNumber in group by, which will result in these two rows. Or, you use an aggregate function to show how to handle the OrderNumbers. For example, MAX(OrderNumber), which means the result is John | 2 or SUM(OrderNumber) which means the result is John | 3.

这篇关于没有聚合函数的 GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)