次の方法で共有


集計正規関数

集計は、一連の入力値を 1 つの値に減らす式です。 通常、集計は SELECT 式の GROUP BY 句と組み合わせて使用され、使用できる場所には制約があります。

集計 Entity SQL 正規関数

Entity SQL の正規関数の集計関数を次に示します。

Avg(expression)

null 以外の値の平均を返します。

引数

Int32Int64Double、および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 

Count(expression)

null 値と重複値を含む集計のサイズを返します。

引数

任意の型。

戻り値

Int32

queryString = @"SELECT VALUE Count(p.ProductID)
    FROM AdventureWorksEntities.Products as p";
SELECT VALUE Count(p.ProductID) 
FROM AdventureWorksEntities.Products AS p 

Max(expression)

null 以外の値の最大値を返します。

引数

ByteInt16Int32Int64ByteSingleDoubleDecimalDateTimeDateTimeOffsetTimeStringBinary

戻り値

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(expression)

null 以外の値の最小値を返します。

引数

ByteInt16Int32Int64ByteSingleDoubleDecimalDateTimeDateTimeOffsetTimeStringBinary

戻り値

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(expression)

null 以外の値の標準偏差を返します。

引数

Int32Int64DoubleDecimal

戻り値

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(expression)

すべての値の母集団の標準偏差を返します。

引数

Int32Int64DoubleDecimal

戻り値

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

Sum(expression)

null 以外の値の合計を返します。

引数

Int32Int64DoubleDecimal

戻り値

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(expression)

null 以外のすべての値の分散を返します。

引数

Int32Int64DoubleDecimal

戻り値

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(expression)

null でないすべての値の母集団の分散を返します。

引数

Int32Int64DoubleDecimal

戻り値

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 クライアント マネージド プロバイダーでも利用できます。 詳細については、「Entity Framework 用 SqlClient 関数」を参照してください。

コレクションベースの集計

コレクションベースの集計 (コレクション関数) はコレクションに対して動作し、値を返します。 たとえば、ORDERS がすべての注文のコレクションである場合は、次の式を使用して最も早い出荷日を計算できます。

min(select value o.ShipDate from LOB.Orders as o)

コレクションベースの集計では、現在の周囲の名前解決スコープ内で式が評価されます。

グループベースの集計

グループベースの集計は、GROUP BY 句で定義されたグループに対して計算されます。 結果のグループごとに、集計計算への入力として各グループの要素を使用して、個別の集計が計算されます。 選択式で group-by 句を使用する場合は、グループ化式名、集計式、または定数式のみをプロジェクション句または order-by 句に含めることができます。

次の例では、各製品に対して注文された平均数量を計算します。

select p, avg(ol.Quantity) from LOB.OrderLines as ol
  group by ol.Product as p

SELECT 式に明示的な group-by 句を指定せずに、グループベースの集計を作成できます。 この場合、すべての要素は 1 つのグループとして扱われます。 これは、定数に基づいてグループ化を指定することと同じです。 たとえば、次の式を使用します。

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 修飾子が指定されている場合 (または修飾子が指定されていない場合)、重複除去は実行されません。

こちらも参照ください