资源预览内容
第1页 / 共39页
第2页 / 共39页
第3页 / 共39页
第4页 / 共39页
第5页 / 共39页
第6页 / 共39页
第7页 / 共39页
第8页 / 共39页
第9页 / 共39页
第10页 / 共39页
亲,该文档总共39页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 1Version No: 1.0Version No: 1.0第7章 数据分组与汇总 使用 TOP n 列出前 n 个记录 使用聚合函数 GROUP BY 的基础知识 在结果集中生成汇总值 使用 COMPUTE 和 COMPUTE BY 子句Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 2Version No: 1.0Version No: 1.0使用 TOP n 列出前 n 个记录n关键字 TOP n 只列出结果集中前 n 个或 前 n% 的记录。使用 TOP n 或 TOP n PERCENT 时,应 注意l在 ORDER BY 子句中指定值的范围l关键字 TOP 后使用无符号的整数l如果 TOP n PERCENT 生成小数,则 SQL Server 将把 这个数取整l可以在结果集中用 WITH TIES 子句包含那些值相 等的记录,这时结果集中可以包含任意数目的行Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 3Version No: 1.0Version No: 1.0使用 TOP n 列出前 n 个记录(续)n统计order details表中每个订单的总销售数量,将 结果集按数量降序排列,并返回前10行。USE northwind SELECT TOP 10 orderid,(unitprice * quantity) as totalsale From order details Order by (unitprice * quantity) DESC GOorderidorderidtotalsaletotalsale 1086515810.00001098115810.0000108617905.0000(10row(s) affected)示例1Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 4Version No: 1.0Version No: 1.0使用 TOP n 列出前 n 个记录(续)USE northwind SELECT TOP 10 WITH TIES orderid,(unitprice * quantity) as totalsale From order details Order by (unitprice * quantity) DESC GO示例2orderidorderidtotalsaletotalsale 1086515810.0000 1098115810.0000 108617905.0000 108177905.0000 (11row(s) affected)Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 5Version No: 1.0Version No: 1.0第7章 数据分组与汇总 使用 TOP n 列出前 n 个记录 使用聚合函数 GROUP BY 的基础知识 在结果集中生成汇总值 使用 COMPUTE 和 COMPUTE BY 子句Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 6Version No: 1.0Version No: 1.0使用聚合函数聚合函数聚合函数描述描述AVG()计算表达式中平均值COUNT()表达式中值的数目COUNT (*)所选择的行的数目MAX()表达式中的最大值MIN()表达式中最小值SUM()计算表达式中所有值的和 常见的聚合函数及其描述Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 7Version No: 1.0Version No: 1.0使用聚合函数(续) 对包含空值的字段使用聚合函数时,应注 意 SQL Server 的聚合函数(COUNT(*) 除外)将忽略字段中的空值 COUNT(*)将计算所有的行,即使每个字段都含有空值Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 8Version No: 1.0Version No: 1.0使用聚合函数(续) 查询titles表中类型是popular_comp的书的平均价格 查询publishers表中供应商的数量 查询publishers表中供应商所在州的数量select avg(price) as 平均价格 from titles where type= popular_compselect count(*) as 供应商数量 from publishersselect count(state) as 供应商所在州的数量 from publishersCopyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 9Version No: 1.0Version No: 1.0使用聚合函数(续) 查询titles表中书的类型有几种select count( type) from titlesselect count( distinct type ) from titles 说明:允许count()、sum()、avg()和 distinct一起处理列或表达式中不同的值Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 10Version No: 1.0Version No: 1.0第7章 数据分组与汇总 使用 TOP n 列出前 n 个记录 使用聚合函数 GROUP BY 的基础知识 在结果集中生成汇总值 使用 COMPUTE 和 COMPUTE BY 子句Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 11Version No: 1.0Version No: 1.0GROUP BY 的基础知识 使用 GROUP BY 子句 联合使用 GROUP BY 子句和 HAVING 子句Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 12Version No: 1.0Version No: 1.0GROUP BY 的基础知识 简单分组 按书的种类分类,求出各类书籍的数量Select type, count(title) as 数量 from titles Group by typeCopyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 13Version No: 1.0Version No: 1.0使用 GROUP BY 子句 联合使用聚合函数和 GROUP BY 子句,能够把表中的记录分组,并对组中数据进行汇总。 使用 GROUP BY 子句时,应注意 对于指定的一组,SQL Server 只生成一条记录,不返回详细信息 不要对可包含空值的字段使用 GROUP BY 子句,因为空值也将被当作一组Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 14Version No: 1.0Version No: 1.0使用 GROUP BY 子句(续)productidproductidorderidorderid quantityquantity11512102110222531153230SELECT productid, SUM(quantity) AS total_quantityFROM orderhistGROUP BY productid GO11125102122102531321530productidproductidtotal_quantitytotal_quantity115235345Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 15Version No: 1.0Version No: 1.0使用 GROUP BY 子句(续)productidproductidorderidorderidquantityquantity11512102110222531153230USE northwind SELECT productid,SUM(quantity) AS total_quantityFROM orderhistWHERE productid 2GROUP BY productid GOproductidproductidtotal_quantitytotal_quantity345对满足 WHERE 子句的行分组先将满足条 件的行选出productidproductid orderidorderidquantityquantity31321530Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 16Version No: 1.0Version No: 1.0使用 GROUP BY 子句(续)USE pubs SELECT royalty, AVG(price * 2) AS AveragePrice FROM pubs.dbo.titles GROUP BY royalty GOroyaltyroyaltyAveragePriceAveragePrice NULLNULL 1032.8880 1230.9400 245.9800示例 对包含空值的字段使用 GROUP BY 子句,空值 也将被当作一组Copyright2006Copyright2006 College of ITSoft (HZIEE) College of ITSoft (HZIEE) 17Version No: 1.0Version No: 1.0使用 GROUP BY 子句(续) 按书的种类分类,求出3种类型书籍 business,mod_cook,trad_cook的价格总和、平均价格以及各类书籍的数量select type,sum(price) as total_price ,avg(price) as avg_price ,count(title) as quantity from titles where type in(business,mod_cook,trad_cook) group by typ
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号