聚合函数: 1.AVG 返回组中的平均,空将被忽略。 例如:use northwind // 操作northwind数据库 Go Select avg (unitprice) //从表中选择求unitprice的平均 From products Where categoryid = ‘8’ 2.BINABY_CHECKSUM 可用于检测表中行的更改返回由表达式
聚合函数:
1.AVG 返回组中的平均值,空值将被忽略。
例如:use northwind // 操作northwind数据库
Go
Select avg (unitprice) //从表中选择求unitprice的平均值
From products
Where categoryid = ‘8’
2.BINABY_CHECKSUM 可用于检测表中行的更改返回值由表达式的运算结果类型决定
例如:use northwind
Go
Create table tablebc(productid int,bchecksum int) //建立有两个属性的表
Insert into tablebc //向表中插入数据
Select priductid,binary_checksum(*)
From products
Update products //更新
Set productname=’oishi tofu’, unitprice=20 where productname = ‘tofu’
Update products //更新
Set priductname=’oishi konbu’, unitprice = 5 where priductname=’konbu’
Update priducts //更新
Set prodctname=’oishi genen shouyu’, unitprice =12
Where priductname=’genen shouyu’
Select productid //挑出变化的行
From tablebc
Where exists (
Select productid from products
Where product.productid = tablebc.productid and
binary_checksu (*) tablebc.bchecksum) //标志出变化的行
3.CHECKSUM 返回在表的行上或在表达式上计算的校验值 CHECKSUM 用于生成哈希索引
例如:
Set arithabort on
se northwind
Go
Alter table products
Add cs_pname as checksum(productname) //在参数列是添加一个校验值
Create imdex pname_index on products(cs_pname) //生成索引
Go
Select top 5 cs_pname from products order by cs_pname desc //选择根据索引列的前5 个cs_pname
4.Checksum_agg 返回组中值的校验值。空值冽被忽略。
例如:
Use northwind
Go
Select checksum_agg(cast(unitsinstock as int)) //检测products 表的unitsinstock列的更改
from products
5.Count 返回组中项目的数量
例如:
例一:
Use pubs
Go
Select count(distinct city) //对city中的每一行都计算,并返回非空的数量
From authors
Go
例二:
Use pubs
Go
Select count(*) // 返回组中项目的数量
From titles
Go
例三:
Use pubs
Go
Select count(*),avg(pr
本文来源gao!%daima.com搞$代*!码网1
ice) // 选择advance大于$1000的总数和平均price
From titles
Where advance >$1000
Go
6.Count_big 返回组中项目的数量。在使用上和count 基本上是一样的,只是在返回值上有一点区别,count_big 返回是bigint的数据类型值,count返回的是int数据类型值。
7.Grouping 返回一个聚合函数,它产生一个附加列,当用CUBE或ROLLUP运算符添加行时,附加 的列输出值为1,当所添加的行不是由CUBE或ROLLUP产生时,附加列值为0
例如:
Use pubs
Go
Select royalty,sum(advance) ‘total advance’, //选择列royalty,聚合 advance数值
Grouping(royalty) ‘grp’ //grouping 函数
From titles
Group by royalty with rollup //group by 与 rollup 相联系产生分组
8.Max 返回表达式的最大值
例如:
Use pubs
Go
Select max(ytd_sales) //选择ytd_sales列属性,求其最大值。
From titles
Go
9.Min 返回表达式的最小值
例如:
Use pubs
Go
Select min(ytd_sales) //选择ytd_sales列属性,求其最小值。
From titles
Go
10.Stdev 返回给定表达式中所有值的统计标准偏差。
例如:
Use pubs
Select stdev(royalty)
From titles
11.Stdevp 返回给定表达式中所有值的填充统计标准偏差。
例如:
Use pubs
Select stdev(royalty)
From titles
12.sum 返回表达式中所有值的的和,或只返回DISTINCT值。SUM只能用数字列。
例如:
Use pubs
Go
Select type,sum(price),sum(advance) //选择type,price的和,advance的和
From titles
Where type like ‘%cook’ //匹配结尾字符为cook
Group by type
Order by type
Go
例如2:
Use pubs
Go
Select type,price,advance
From titles
Where type like’%cook’
Order by type
Compute sum(price),sum(advance) by type //根据type的属性计算price和advance
的和
13.Var 返回给定表达式中所有值的统计方差。
例如:
Use pubs
Go
Selecdt var(royalty)
From titles
14.Varp 返回给定表达式中所有值的填充的统计方差
例如:
Use pubs
Go
Select varp(royalty)
From titles