SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>sql中聚合函数和分组函数_sql选择计数聚合函数-语法示例解释</li><li>
sql语句聚合函数和分组操作的注意事项</li><li>
sql聚合函数和分组数据语法及概述</li></ul></div><p>
<strong>1 分组聚合的原因</strong></p>
<p>
sql中分组函数和聚合函数之前的文章已经介绍过,单说这两个函数有可能比较好理解,分组函数就是group by,聚合函数就是count、max、min、avg、sum。</p>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/09079cb6270470bafe85390ad5fa7973.jpg"></p>
<p>
拿上图中的数据进行解释,假设按照product_type这个字段进行分组,分组之后结果如下图。</p>
<blockquote>
select product_type from productgroup by product_type</blockquote>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/58b1dfe2e158f61be823310e113c1144.jpg"></p>
<p>
从图中可以看出被分为了三组,分别为厨房用具、衣服和办公用品,就相当于对product_type这个字段进行了去重,确实group by函数有去重的作用。</p>
<blockquote>
select distinct product_type from product</blockquote>
<p>
假设分组之后,我想看一下价格,也就是sale_price这个字段的值,按照如下这个写法,会报如下错误。</p>
<blockquote>
select product_type,sale_price from productgroup by product_type</blockquote>
<p>
</p>
<p>
这是为什么呢?原表按照product_type分组之后,厨房用具对应4个值,衣服对应2个值,办公用品对应2个值,这就是在取sale_price这个字段的时候为什么报错了,一个空格中不能填入多个值,这时候就可以用聚合函数了,比如求和,求平均,求最大最小值,求行数。聚合之后的值就只有一个值了。</p>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/5f1d1c31db2ae3e959a0cedab0296843.jpg"></p>
<blockquote>
select product_type,sum(sale_price),avg(sale_price),count(sale_price),max(sale_price) from productgroup by product_type</blockquote>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/f6e88867d699d65baaaaf4207ff1793d.jpg"></p>
<p>
对于多个字段的分组,其原理是一样的。从上述中记住两点:分组去重和分组聚合。</p>
<p>
<strong>2 distinct和group by去重的区别 distinct 和group by 设计时侧重点不一样</strong></p>
<p>
distinct只是为了去重,而group by是为了聚合统计的。</p>
<p>
两者都有去重的效果,但是执行的效率不一样</p>
<p>
单个字段去重</p>
<blockquote>
--distinctselect distinct product_type from product--group byselect product_type from productgroup by product_type</blockquote>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/bc129337feeb1ce62c142bed86c925fa.jpg"></p>
<p>
<strong>多个字段去重</strong></p>
<blockquote>
--distinctselect distinct product_name, product_type from product--group byselect product_name, product_type from productgroup by product_name, product_type</blockquote>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/fc828d7660661aa9a6724393c42d8a8d.jpg"></p>
<p>
<strong>执行效率</strong></p>
<blockquote>
select <列名1>,<列名2>from<表名>where 查询条件group by 分组类别having 对分组结果指定条件order by <列名> (desc)limit 数字</blockquote>
<p>
<img title="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" alt="SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明" src="https://zhuji.jb51.net/uploads/img/202305/564ce05a375cc9f64643d10940ae4075.jpg"></p>
<p>
sql语言的运行顺序,先执行上图中的第一步,然后再执行select子句,最后对结果进行筛选。distinct是在select子句中,而group by在第一步中,所以group by去重比distinct去重在效率上要高。</p>
<p class="maodian"></p><h2>
sql中聚合函数和分组函数_sql选择计数聚合函数-语法示例解释</h2>
<p>
<strong>sql中聚合函数和分组函数</strong></p>
<p>
the count operator is usually used in combination with a group by clause. it is one of the sql “aggregate” functions, which include avg (average) and sum.</p>
<p>
count运算符通常与group by子句结合使用。 它是sql“聚合”功能之一,其中包括avg(平均)和sum。</p>
<p>
this function will count the number of rows and return that count as a column in the result set.</p>
<p>
此函数将对行数进行计数,并将该计数作为列返回到结果集中。</p>
<p>
here are examples of what you would use count for:</p>
<p>
以下是将count用于以下用途的示例:</p>
<p>
counting all rows in a table (no group by required)<br>
计算表中的所有行(不需要按组)<br>
counting the totals of subsets of data (requires a group by section of the statement)<br>
计算数据子集的总数(需要语句的“分组依据”部分)<br>
for reference, here is the current data for all the rows in our example student database.</p>
<p>
作为参考,这是示例学生数据库中所有行的当前数据。</p>
<blockquote>
<p>
select studentid, fullname, programofstudy, sat_score from student; -- all records with fields of interest<br>
this sql statement provides a count of all rows. note that you can give the resulting count column a name using “as”.</p>
</blockquote>
<p>
该sql语句提供所有行的计数。 请注意,您可以使用“ as”为所得的count列命名。</p>
<blockquote>
<p>
select count(*) as studentcount from student; -- count of all records</p>
</blockquote>
<p>
here we get a count of students in each field of study.</p>
<p>
在这里,我们得到了每个学习领域的学生人数。</p>
<blockquote>
<p>
select studentid, fullname, count(*) as studentcount from the student table with a group by programofstudy;</p>
</blockquote>
<p>
here we get a count of students with the same sat scores.</p>
<p>
在这里,我们得到了具有相同sat分数的学生人数。</p>
<blockquote>
<p>
select studentid, fullname, count(*) as studentcount from the student table with a group by sat_score;</p>
</blockquote>
<p>
here is an example using the campaign funds table. this is a sum total of the dollars in each transaction and the number of contributions for each political party during the 2016 us presidential campaign.</p>
<p>
这是使用广告系列资金表的示例。 这是2016年美国总统大选期间每笔交易的总金额和每个政党的捐款额。</p>
<div class="jb51code">
<div>
<div class="syntaxhighlightersql" id="highlighter_98289">
<div class="toolbar">
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
<div class="line number1 index0 alt2">
1</div>
<div class="line number2 index1 alt1">
2</div>
<div class="line number3 index2 alt2">
3</div>
<div class="line number4 index3 alt1">
4</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="sql keyword">select</code> <code class="sql plain">specific_party, election_year, format(</code><code class="sql color2">sum</code><code class="sql plain">(total_$),2) </code><code class="sql keyword">as</code> <code class="sql plain">contribution$total, </code><code class="sql color2">count</code><code class="sql plain">(*) </code><code class="sql keyword">as</code> <code class="sql plain">numberofcontributions </code>
</div>
<div class="line number2 index1 alt1">
<code class="sql keyword">from</code> <code class="sql plain">combined_party_data</code>
</div>
<div class="line number3 index2 alt2">
<code class="sql keyword">group</code> <code class="sql keyword">by</code> <code class="sql plain">specific_party,election_year</code>
</div>
<div class="line number4 index3 alt1">
<code class="sql keyword">having</code> <code class="sql plain">election_year = 2021;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
</div>
<p>
as with all of these things there is much more to it, so please see the manual for your database manager and have fun trying different tests yourself.</p>
<p>
关于所有这些事情,还有很多事情要做,所以请参阅数据库管理员手册,并尝试自己进行不同的测试,这很有趣。</p>
<p class="maodian"></p><h2>
sql语句聚合函数和分组操作的注意事项</h2>
<p>
group by可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个汇总表。</p>
<p>
group by几个比较重要的约束:</p>
<p>
(1)select字句中的列名和having或where中的列名必须为分组列或列函数.列函数对于group by字句定义的每个组返回一个结果<br>
(2)group by一般和聚合函数一使用才有意义,比如count,sum,avg等,使用group by 的两个要素:<br>
(3)出现在select后面的字段,要么是聚合函数中的,要么是group by中的.<br>
(4)要筛选结果,可以先使用where再用group by或者先用group by再用having</p>
<p>
第(4)项根据各个数据库不同不一定都能适用,因此最好不要这样用,老老实实用having</p>
<p class="maodian"></p><h2>
sql聚合函数和分组数据语法及概述</h2>
<p>
(一)聚合函数是指对列上的数据进行操作,起到统计的作用。前面的函数都是最一行记录进行操作得到的数据,包括前面的日期函数、数学函数、字符函数、转换函数以及条件判断函数。</p>
<p>
常用的聚合函数有:count、sum、avg、max、min。这5个函数个起到统计记录数、求和、求平均值、求最大值、最小值的作用。</p>
<p>
count:count函数对查询的数据统计记录数量,这个函数不对字段值为null的值进行统计,也就是说某个查询的字段有null值,则null值的数量会被减除,这样就可以不对null设置查询条件了。<br>
如果要对null值设置查询,则可以用where 字段 is null来作为条件。<br>
sum:sum函数求和,只能对数值型数据操作,也会忽略null值。举例:<br>
select sum(考试成绩) as 计算机总成绩<br>
from score<br>
where 课号 in (select 课号 from course where 课名=”计算机”)<br>
avg:求平均值,参数也必须为数值型字段名或者结果为数值的表达式。<br>
max、min:这两个函数求最大值和最小值,但是不能放到wherer中以及select子句的字段名位置上。<br>
例:select max(x1) from y where max(x2) in(select…) 错误的语法。<br>
select x1 from y where x2=max(x3) 错误的语法。<br>
select max(x1) from y where x2) in(select max(x2,)…) 正确。<br>
注:5个函数都可以使用distinct统计不重复的值:<br>
select count(distinct(课程)) as 课程数量 from 课程表<br>
access和mysql不能将distinct放置到参数中,解决方法:查询distinct保存为新表into语句,然后再使用count。</p>
<p>
(二)数据分组是指将数据表中的数据按照指定字段的不同值分为很多组,使用group by 子句进行操作。</p>
<p>
group by通常不直接查询所有字段并且分组,group by和select后分组字段和查询字段通常一致。因为select * from y group by x 会产生错误,通常对某个字段分组并且利用聚合函数计算分组得到的值。<br>
聚合函数和分组的组合并设置查询条件:可以对一个字段分组并且设定条件,这样得到的结果将会是计算分组并且满足条件的值。例:<br>
查询各个所属院系中所有男生的值:<br>
select 所属院系,count(*) as 男生人数from student where 性别='男' group by 所属院系<br>
查询直方图:利用replicate()函数,将分组得到的数据作为次数,重复一个设置的符号。<br>
排序查询结果:order by语句,asc、desc 位于group by之后<br>
case表达式和group by的结合:<br>
select 所属院系,count(case<br>
when 性别='男' then 1<br>
else null<br>
end ) as 男生人数,<br>
count(case<br>
when 性别='女' then 1<br>
else null<br>
end ) as 女生人数<br>
from student group by 所属院系<br>
hanving子句设置分组group by的分组查询条件。<br>
having子句总是和group by子句结合使用,依赖于分组,可以在having中使用聚合函数;where 也可以设定条件,但是不依赖于分组的字段,但是不能使用聚合函数。<br>
select 学号,sum(考试成绩) as 考试总成绩 from score group by 学号 having sum(考试成绩)>400 order by 考试总成绩 desc</p>
<p>
到此这篇关于sql分组函数group by和聚合函数(count、max、min、avg、sum)的几点说明的文章就介绍到这了,更多相关sql分组函数和聚合函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!</p>
<p>
原文链接:https://blog.csdn.net/qq_26735495/article/details/110346502</p>
頁:
[1]