sqlserver中的分组函数

sqlserver中的分组函数

ID:34725936

大小:80.18 KB

页数:3页

时间:2019-03-10

sqlserver中的分组函数_第1页
sqlserver中的分组函数_第2页
sqlserver中的分组函数_第3页
资源描述:

《sqlserver中的分组函数》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、分组统计数据说明:groupby是sql中对数据表中的数据进行分组的,在select列表中出现的字段必须全部出现在groupby字段中,出现在聚合函数中的字段在groupby中可有可无,没有出现在select列表中的字段在groupby中也可以使用。在groupby中不可以使用列别名。语法:selectcolumn_name,aggregate_function(column_name)fromtable_namewherecolumn_nameoperatorvaluegroupbycolumn_name(1)分组计算数据a.本实例利用sum()函数和groupby计算图书销售表(Boo

2、ksales)中图书的总销售额selectb_code,sum(b_price)fromBooksalesgroupbyb_codeb.本实例利用avg()函数和groupby计算学生表信息(studenttable)中男生和女生的平均年龄selectstudentsex,avg(studentage)fromstudenttablegroupbystudentsexc.本实例利用max()函数和groupby计算学生信息表(studenttable)中男生和女生的最大年龄selectstudentsex,max(studentage)fromstudenttablegroupbystud

3、entsexd.本实例利用min()函数和groupby计算学生信息表(studenttable)中男生和女生的最小年龄selectstudentsex,min(studentage)fromstudenttablegroupbystudentsex(2)groupbyandall说明:本实例中利用了groupby子句和all关键字,在groupby子句中使用all关键字,只有在sql语句中包含where子句时,all才有意义。a.查询图书销售表(Booksales)中图书编号为1100010101的图书销售总额,且列出其他图书编号Selectb_code,sum(sal_tot)from

4、Booksaleswhereb_code=’1100010101’groupbyallb_code(3)ROLLUP的使用说明:ROLLUP关键字是用来生成小计的,利用了withrollup关键字会在结果集的最后显示的行名称为空,而后面对应的值则为计算列的所有值!a.利用sum()和withrollup对学生信息表(studenttable)中的所有年龄生成小计Selectstudentsex,sum(studentage)fromstudenttablegroupby[studentsex]withrollupb.利用max()和withrollup对学生信息表(studenttable

5、)中的年龄最大值生成小计Selectstudentsex,max(studentage)fromstudenttablegroupby[studentsex]withrollupc.利用min()和withrollup对学生信息表(studenttable)中的年龄最小值生成小计selectstudentsex,min(studentage)fromstudenttablegroupby[studentsex]withrollupd.利用avg()和withrollup对学生表(studenttable)中的年龄平均值生成小计Selectstudentsex,avg(studentage)

6、fromstudenttablegroupby[studentsex]withrollupe.利用count()和withrollup对学生表(studenttable)中的记录数生成小计Selectstudentsex,count(*)fromstudenttablegroupby[studentsex]withrollup注意:当withrollup与sum()一起使用时得出的结果是分组后每组的和的和当withrollup与max()一起使用时得出的结果是分组后组的较大值当withrollup与min()一起使用时得出的结果是分组后组的较小值当withrollup与avg()一起使用时

7、得出的结果是所以记录的平均值当withrollup与count()一起使用时得出的结果是分组后各组数量的总和(4)CUBE的使用说明:CUBE用来生成小计和总计交叉表,groupby分组后由CUBE生成总计和小计!a.利用sum()和CUBE对图书销售表(Booksales)中的销售额生成小计和总计Selectb_code,b_number,sum(sal_tot)fromBooksalesgroupbyb_code,b

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。