聚合是将一系列输入值简化为例如单个值的表达式。 聚合通常与 SELECT 表达式的 GROUP BY 子句结合使用,并且可以使用它们的位置存在约束。
聚合实体 SQL 规范函数
以下是聚合实体 SQL 规范函数。
Avg(expression)
返回非 null 值的平均值。
论点
              Int32、Int64、Double和Decimal。
返回值
如果所有输入值均为expression值,则类型为null,否则为null类型。
示例
queryString = @"SELECT VALUE AVG(p.ListPrice)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE AVG(p.ListPrice) 
FROM AdventureWorksEntities.Products AS p 
BigCount(表达式)
返回聚合的大小,包括 null 值和重复值。
论点
任何类型。
返回值
一个 Int64。
示例
queryString = @"SELECT VALUE BigCount(p.ProductID)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE BigCount(p.ProductID) 
FROM AdventureWorksEntities.Products AS p 
计数(表达式)
返回聚合的大小,包括 null 值和重复值。
论点
任何类型。
返回值
一个 Int32。
示例
queryString = @"SELECT VALUE Count(p.ProductID)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE Count(p.ProductID) 
FROM AdventureWorksEntities.Products AS p 
Max(表达式)
返回非 null 值的最大值。
论点
AByte、Int16、Int32、Int64、Byte、Single、Double、Decimal、DateTime、DateTimeOffset、Time、String、Binary.
返回值
如果所有输入值均为expression值,则类型为null,否则为null类型。
示例
queryString = @"SELECT VALUE MAX(p.ListPrice)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE MAX(p.ListPrice) 
FROM AdventureWorksEntities.Products AS p 
Min(表达式)
返回非 null 值的最小值。
论点
AByte、Int16、Int32、Int64、Byte、Single、Double、Decimal、DateTime、DateTimeOffset、Time、String、Binary.
返回值
如果所有输入值均为expression值,则类型为null,否则为null类型。
示例
queryString = @"SELECT VALUE MIN(p.ListPrice)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE MIN(p.ListPrice) 
FROM AdventureWorksEntities.Products AS p 
StDev(表达式)
返回非 null 值的标准偏差。
论点
              Int32、Int64、Double、Decimal。
返回值
              Double。 
              Null(如果所有输入值都是 null 值)。
示例
queryString = @"SELECT VALUE StDev(product.ListPrice)
    FROM AdventureWorksEntities.Products AS product
    WHERE product.ListPrice > @price";
SELECT VALUE StDev(product.ListPrice) 
FROM AdventureWorksEntities.Products AS product 
WHERE product.ListPrice > @price
StDevP(表达式)
返回所有值的总体标准偏差。
论点
              Int32、Int64、Double、Decimal。
返回值
如果所有输入值均为 Double 值,则为 null 或 null。
示例
queryString = @"SELECT VALUE StDevP(product.ListPrice)
    FROM AdventureWorksEntities.Products AS product
    WHERE product.ListPrice > @price";
SELECT VALUE StDevP(product.ListPrice) 
FROM AdventureWorksEntities.Products AS product 
WHERE product.ListPrice > @price
求和(表达式)
返回非 null 值的总和。
论点
              Int32、Int64、Double、Decimal。
返回值
如果所有输入值均为 Double 值,则为 null 或 null。
示例
queryString = @"SELECT VALUE Sum(p.ListPrice)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE Sum(p.ListPrice) 
FROM AdventureWorksEntities.Products AS p 
Var(表达式)
返回所有非 null 值的方差。
论点
              Int32、Int64、Double、Decimal。
返回值
如果所有输入值均为 Double 值,则为 null 或 null。
示例
queryString = @"SELECT VALUE Var(product.ListPrice)
    FROM AdventureWorksEntities.Products AS product
    WHERE product.ListPrice > @price";
SELECT VALUE Var(product.ListPrice) 
FROM AdventureWorksEntities.Products AS product 
WHERE product.ListPrice > @price
VarP(表达式)
返回所有非 Null 值的总体方差。
论点
              Int32、Int64、Double、Decimal。
返回值
如果所有输入值均为 Double 值,则为 null 或 null。
示例
queryString = @"SELECT VALUE VarP(product.ListPrice)
    FROM AdventureWorksEntities.Products AS product
    WHERE product.ListPrice > @price";
SELECT VALUE VarP(product.ListPrice) 
FROM AdventureWorksEntities.Products AS product 
WHERE product.ListPrice > @price
Microsoft SQL 客户端托管提供程序中提供了等效功能。 有关详细信息,请参阅实体框架函数的 SqlClient。
基于集合的聚合
基于集合的聚合(集合函数)对集合进行作并返回值。 例如,如果 ORDERS 是所有订单的集合,则可以使用以下表达式计算最早的发货日期:
min(select value o.ShipDate from LOB.Orders as o)
将在当前环境名称解析范围内计算基于集合的聚合内的表达式。
基于组的聚合
基于组的聚合将按照 GROUP BY 子句定义的方式对组进行计算。 对于结果中的每个组,将使用每个组中的元素作为聚合计算的输入来计算单独的聚合。 当在 select 表达式中使用 group-by 子句时,在投影或 order-by 子句中只存在分组表达式名称、聚合或常量表达式。
以下示例计算每个产品订购的平均数量:
select p, avg(ol.Quantity) from LOB.OrderLines as ol
  group by ol.Product as p
在 SELECT 表达式中,可以在没有显式 group-by 子句的情况下使用基于组的聚合。 在这种情况下,所有元素都被视为单个组。 这相当于根据常量指定分组。 例如,采用以下表达式:
select avg(ol.Quantity) from LOB.OrderLines as ol
这等效于以下各项:
select avg(ol.Quantity) from LOB.OrderLines as ol group by 1
将在 WHERE 子句表达式可见的名称解析范围内计算基于组的聚合中的表达式。
与 Transact-SQL 中一样,基于组的聚合还可以指定 ALL 或 DISTINCT 修饰符。 如果指定 DISTINCT 修饰符,则会在计算聚合之前从聚合输入集合中消除重复项。 如果指定了 ALL 修饰符(或者未指定任何修饰符),则不执行重复消除。