Home  >  Article  >  Database  >  sql GROUP BY用法

sql GROUP BY用法

WBOY
WBOYOriginal
2016-06-07 17:48:001901browse

group函数是对数据集按照某个字段或者表达式进行分组,获得一组组的集合,然后从每组中取出一个
指定字段或者表达式的值,放到单元格中进行扩展,扩展出来的每个单元格都保留了一个指针指向当前的组集,
该组集称为当前组。

       因此在附属单元格中,需要对该组集进行操作时,不需要用任何条件和主单元格关联了,如果加设了条件,反倒画蛇添足,导致报表引擎还对组集中的记录进行遍历检索。

 

正确的group 用法:

 

1.jpg

不合理的group用法:

 

 

2.jpg

 

 

group函数的原理图示:

3.jpg

 group by 语句用于结合合计函数,根据一个或多个列对结果集进行分组。

sql group by 语法
select column_name, aggregate_function(column_name)
from table_name
where column_name operator value
group by column_name

group by 一个以上的列
我们也可以对一个以上的列应用 group by 语句,就像这样:

select customer,orderdate,sum(orderprice) from orders
group by customer,orderdate

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn