聚合函数 (Entity SQL)

SQL Server .NET Framework 数据提供程序 (SqlClient) 提供聚合函数。聚合函数对一组输入值执行计算并返回一个值。这些函数位于 SqlServer 命名空间中,该命名空间在您使用 SqlClient 时可用。提供程序的命名空间属性使实体框架可以确定此提供程序对特定构造(如类型和函数)使用哪个前缀。

下表显示 SqlClient 聚合函数。

函数 说明

AVG( expression )

返回集合中各值的平均值。

将忽略 Null 值。

参数

Int32 Int64DoubleDecimal

返回值

expression 的类型。

示例

SELECT VALUE SqlServer.AVG(p.ListPrice) FROM

AdventureWorksEntities.Product as p

CHECKSUM_AGG( collection )

返回集合中各值的校验和。

将忽略 Null 值。

参数

集合 (Int32)。

返回值

Int32

示例

SELECT VALUE SqlServer.Checksum_Agg(cast(product.ListPrice as Int32))

FROM AdventureWorksEntities.Product AS product

where product.ListPrice > cast(2.0 as Decimal)

COUNT( expression )

Int32 形式返回集合中的项数。

参数

集合 (T),其中 T 为以下类型之一:

Guid(在 SQL Server 2000 中不返回)、

BooleanDoubleDateTimeDateTimeOffsetTimeStringBinary

返回值

Int32

示例

anyelement(SELECT VALUE SqlServer.COUNT(product.ProductID)

FROM AdventureWorksEntities.Product AS product

WHERE SqlServer.CEILING(product.ListPrice) ==

SqlServer.FLOOR(product.ListPrice))

COUNT_BIG( expression )

bigint 形式返回集合中的项数。

参数

集合 (T),其中 T 为以下类型之一:

Guid(在 SQL Server 2000 中不返回)、BooleanDoubleDateTimeDateTimeOffsetTimeStringBinary

返回值

Int64

示例

SELECT VALUE SqlServer.COUNT_BIG(product.ProductID)

FROM AdventureWorksEntities.Product AS product

WHERE SqlServer.CEILING(product.ListPrice) ==

SqlServer.FLOOR(product.ListPrice)

MAX( expression )

返回集合中的最大值。

参数

集合 (T),其中 T 为以下类型之一:ByteInt16Int32Int64ByteSingleDoubleDecimalDateTimeDateTimeOffsetTimeStringBinary

返回值

expression 的类型。

示例

SELECT VALUE SqlServer.MAX(p.ListPrice)

FROM AdventureWorksEntities.Product as p

MIN( expression )

返回集合中的最小值。

参数

集合 (T),其中 T 为以下类型之一:ByteInt16Int32Int64ByteSingleDoubleDecimalDateTimeDateTimeOffsetTimeString

Binary

返回值

expression 的类型。

示例

SELECT VALUE SqlServer.MIN(p.ListPrice)

FROM AdventureWorksEntities.Product as

STDEV( expression )

返回指定表达式中所有值的标准偏差。

参数

集合 (Double)。

返回值

Double

示例

SELECT VALUE SqlServer.STDEV(product.ListPrice)

FROM AdventureWorksEntities.Product AS product

where product.ListPrice > cast(2.0 as Decimal)

STDEVP( expression )

返回指定表达式中所有值的总体标准偏差。

参数

集合 (Double)。

返回值

Double

示例

SELECT VALUE SqlServer.STDEVP(product.ListPrice)

FROM AdventureWorksEntities.Product AS product

where product.ListPrice > cast(2.0 as Decimal)

SUM( expression )

返回集合中所有值的总和。

参数

集合 (T),其中 T 为以下类型之一:Int32Int64DoubleDecimal

返回值

expression 的类型。

示例

SELECT VALUE SqlServer.SUM(p.ListPrice)

FROM AdventureWorksEntities.Product as p

VAR( expression )

返回指定表达式中所有值的方差。

参数

集合 (Double)。

返回值

Double

示例

SELECT VALUE SqlServer.VAR(product.ListPrice)

FROM AdventureWorksEntities.Product AS product

where product.ListPrice > cast(2.0 as Decimal)

VARP( expression )

返回指定表达式中所有值的总体方差。

参数

集合 (Double)。

返回值

Double

示例

SELECT VALUE SqlServer.VARP(product.ListPrice)

FROM AdventureWorksEntities.Product AS product

where product.ListPrice > cast(2.0 as Decimal)

有关 SqlClient 支持的聚合函数的更多信息,请参见 SqlClient 提供程序清单中所指定的 SQL Server 版本的相应文档:

另请参见

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...