zoukankan      html  css  js  c++  java
  • sql server 分组统计数据

    说明:group by是sql中对数据表中的数据进行分组的,在select列表中出现的字段必须全部出现在group by 字段中,出现在聚合函数中的字段在group by中可有可无,没有出现在select列表中的字段在group by中也可以使用。在group by中不可以使用列别名。
    语法:select column_name,aggregate_function(column_name) from table_name where column_name operator value group by column_name
    (1)分组计算数据
    a.本实例利用sum()函数和group by计算图书销售表(Booksales)中图书的总销售额
    select b_code,sum(b_price) from Booksales group by b_code
    b.本实例利用avg()函数和group by 计算学生表信息(studenttable) 中男生和女生的平均年龄
    select studentsex,avg(studentage) from studenttable group by studentsex
    c.本实例利用max()函数和group by 计算学生信息表(studenttable)中男生和女生的最大年龄
    select studentsex,max(studentage) from studenttable group by studentsex
    d.本实例利用min()函数和group by 计算学生信息表(studenttable)中男生和女生的最小年龄
    select studentsex,min(studentage) from studenttable group by studentsex
    (2)group by and all
    说明:本实例中利用了group by子句和all关键字,在group by 子句中使用all关键字,只有在sql语句中包含where子句时,all才有意义。
    a. 查询图书销售表(Booksales)中图书编号为1100010101的图书销售总额,且列出其他图书编号
    Select b_code,sum(sal_tot) from Booksales where b_code=’1100010101’ group by all b_code
    (3) ROLLUP的使用
    说明:ROLLUP关键字是用来生成小计的,利用了with rollup关键字会在结果集的最后显示的行名称为空,而后面对应的值则为计算列的所有值!
    a. 利用sum()和with rollup对学生信息表(studenttable)中的所有年龄生成小计
    Select studentsex,sum(studentage) from studenttable group by [studentsex] with rollup
    b. 利用max()和with rollup 对学生信息表(studenttable) 中的年龄最大值生成小计
    Select studentsex,max(studentage) from studenttable group by [studentsex] with rollup
    c.利用min()和with rollup对学生信息表(studenttable)中的年龄最小值生成小计
    select studentsex,min(studentage) from studenttable group by [studentsex] with rollup
    d. 利用avg()和with rollup 对学生表(studenttable)中的年龄平均值生成小计
    Select studentsex,avg(studentage) from studenttable group by [studentsex] with rollup
    e. 利用count()和with rollup对学生表(studenttable)中的记录数生成小计
    Select studentsex,count(*) from studenttable group by [studentsex] with rollup
    注意:
    当with rollup与sum()一起使用时得出的结果是分组后每组的和的和
    当with rollup与max()一起使用时得出的结果是分组后组的较大值
    当with rollup与min()一起使用时得出的结果是分组后组的较小值
    当with rollup与avg()一起使用时得出的结果是所以记录的平均值
    当with rollup与count()一起使用时得出的结果是分组后各组数量的总和
    (4)CUBE的使用
    说明:CUBE用来生成小计和总计交叉表,group by 分组后由CUBE生成总计和小计!
    a. 利用sum()和CUBE对图书销售表(Booksales)中的销售额生成小计和总计
    Select b_code,b_number,sum(sal_tot) from Booksales group by b_code,b_number with cube
    b. 利用max()和CUBE对图书销售表(Booksales)中的销售额生成小计和总计
    Select b_code,b_number,max(sal_tot) from Booksales group by b_code,b_number with cube
    c. 利用min()和CUBE对图书销售表(Booksales)中的销售额生成小计和总计
    Select b_code,b_number,min(sal_tot) from Booksales group by b_code,b_number with cube
    d. 利用avg()和cube对图书销售表(Booksales)中的销售额生成小计和总计
    Select b_code,b_number,avg(sal_tot) from Booksales group by b_code,b_number with cube
    e. 利用count()和cube对图书销售表(Booksales)中的销售额生成小计和总计
    Select b_code,b_number,count(*)from Booksales group by b_code,b_number with cube
    (5)where与having的使用
    说明:where子句是对select语句的结果进行筛选,having子句则是对group by子句进行筛选,having子句通常和group by子句一起使用,如果不使用group by子句,则having子句的行为和where子句的行为一样,当having和group by all一起使用时,having子句代替all,在having子句中不能使用text,image和ntext类型,where子句是在进行分组之前应用,而having子句则是在分组后应用,having子句可以包含聚合函数,也可以引用选择列表中出现的任何选项,也可以应用group by中的任何选项,having也可引用没有出现在select列表的而出现在group by列表的字段。
    a. 使用group by与having查询图书销售表(Booksales)中图书销售额大于200的图书编号和销售额
    Select b_code,sum(b_price) from Booksales group by b_code having sum(p_price)> 200
    b. 使用group by 与having查询图书销售表(Booksales)中图书销售记录大于2条的图书编号和销售额
    Select b_code,sum(b_price) from Booksales group by b_code having count(*)>2
    c. 使用group by与having查询销售数量大于2并且销售额大于50的图书编号和销售额按销售额降序
    Select b_code,sum(b_price) from Booksales where b_number>2 group by b_code having sum(b_price)>50 order by sum(b_price) desc
    d. 使用group by与having查询联系方式表(contact)中年龄不为20的id号
    select id from contact group by id,ages having ages not in (20)
    (6)compute与compute by的使用方法
    说明:compute是对聚合函数生成小计的,一个select语句中可以有多个compute指定的聚合函数,注意在compute子句的聚合函数中不可以使用列别名,compute输出的结果为两行,
    第一行:生成select_list列表中行的明细
    第二行:compute子句中聚合函数的小计
    Compute by 则是先实现分组然后统计compute子句中聚合函数小计的,在compute by语句中必须要有order by排序并且排序列和by分组列必须相同
    第一行:生成select_list列表中行的明细
    第二行:compute子句中聚合函数的小计
    两个都是输出compute子句中聚合函数的小计,但是不相同的是compute输出的是聚合列在表中的所有值,而compute by 则是输出先按by子句中的字段分组以后每组的聚合列的值!
    a. 查询图书销售表(Booksales)中的总销售额,销售个数
    Select * from Booksales compute sum(sal_tot),sum(b_number)
    b. 查询图书销售表(Booksales)中的销售额最小和最大并按图书编号升序排列
    Select * from Booksales order by b_code compute max(sal_tot),min(sal_tot)
    c. 查询图书销售表(Booksales)中的总销售额,销售个数按b_code分组
    select * from Booksales order by b_code compute sum(sal_tot),sum(b_number) by b_code

    (7)group by 和 compute by 的比较
    说明:group by与compute by都可以进行分组,但是他们有一定的区别
    Group by :生成单个结果集,每一行都只有分组依据列和聚合列,选择列表也只能包含分组依据列和聚合列,可排序也可不排序,如果要排序的话只能先分组后排序,排序列必须和分组列相同或者是分组列的子集或者为聚合函数产生的新列,聚合函数写在select列表中,在group by 子句中不可以使用聚合函数、在聚合函数聚合结果中可以使用列别名。
    Compute by:生成多个结果集,一种结果集包括选择列表中的行的明细,另一种结果集则包含聚合以后的小计,在compute by 子句中必须排序,并且排序列必须和compute by的中分组列相同,聚合函数写在compute子句中,分组列写在by子句中,分组列可以是表中的任意列,但不可以使用列别名,在聚合函数聚合结果中不可以使用列别名。

  • 相关阅读:
    反馈更多的信息,让别人感到舒服。
    Centos 安装Redis
    CentOS用yum安装MySQL 8.0 .
    MySQL事务。
    Java垃圾回收。
    类加载机制与类加载器。
    Java内存模型。
    844--Backspace String Compare
    maven的配置及基本操作
    idea基本使用
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3181728.html
Copyright © 2011-2022 走看看