FetchXML 集計の使用

Microsoft Dataverse で、FetchXML にはグループ化および集計の機能が含まれています。この機能を使用すると、合計、平均、最小値、最大値、および件数を計算できます。

次の集計機能がサポートされています。

  • sum
  • avg
  • max
  • count(*)
  • count(属性名)

集計について

集計値を返すには、fetch 要素 aggregate 属性を trueに設定します。 次に、entity 要素内で、nameaggregatealias 要素を持つ attribute 要素を複数設定します。 aggregate 属性を使用して、希望する集計の種類を指定します。

次の例は、FetchXML の簡単な aggregate クエリを示しています。

<fetch distinct='false' mapping='logical' aggregate='true'>   
   <entity name='entity name'>   
      <attribute name='attribute name' aggregate='count' alias='alias name'/>   
   </entity>   
</fetch>

aggregated 列を使用したクエリの結果は、標準のクエリの結果とは異なります。 alias 属性の値は、集計結果の識別子として使用されます。

次の例は、集計クエリの結果の形式を示しています。

<resultset morerecords="0"'>   
   <result>  
      <alias>aggregate value</alias>  
   </result>  
</resultset>

次の例は、alias 変数が account_count に設定された場合のクエリの結果を示しています。

<resultset morerecords="0"'>   
   <result>  
      <account_count>20</account_count>  
   </result>  
</resultset>

制限

返される集計値が 50,000 レコードに限定されたクエリ。 この最大値で、システムの動作および信頼性を保持することができます。 クエリ内のフィルター条件に 50,000 を超えるレコードが含まれる場合は次のエラーが表示されます。

エラー コード: -2147164125
16 進値のエラー コードです。8004E023
プラットフォーム エラー メッセージ: AggregateQueryRecordLimit exceeded. Cannot perform this operation.
クライアント エラー メッセージ: 最大レコード制限を超えています。 レコード数を減らしてください。

このエラーを回避するには、適切なフィルターをクエリに追加して、50,000 超のレコードを評価する必要がないことを確認します。 その後、クエリを何度か実行して結果をまとめます。

ヒント

フィルターを使わずにレコードの総数を取得するには、Web API RetrieveTotalRecordCount function または SDK for .NET RetrieveTotalRecordCountRequest class を用いて RetrieveTotalRecordCount メッセージを使用します。 取得されるデータは、過去 24 時間以内のスナップ ショットからのものになります。

平均

次の例は、aggregate 属性を使用して avg 集計関数を使う方法を示しています。

// Fetch the average of estimatedvalue for all opportunities.  This is the equivalent of 
// SELECT AVG(estimatedvalue) AS estimatedvalue_avg ... in SQL.
string estimatedvalue_avg = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg' /> 
   </entity> 
</fetch>";

EntityCollection estimatedvalue_avg_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_avg));

foreach (var c in estimatedvalue_avg_result.Entities)
{
   decimal aggregate1 = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average estimated value: " + aggregate1);

}

平均を計算する場合の NULL 値による制限

Dataverse でデータの平均を計算する場合、Null 値は考慮されません。 ただし、ゼロ (0) は使用されます。

次の例のデータの場合、取引先企業 1 (2 つのエントリ) の平均は 250 です。これに対し、取引先企業 2 (2 つのエントリ) の平均は 125 です。

営業案件 見込み顧客 見込み値
営業案件 1 取引先企業 1 null
営業案件 2 取引先企業 1 250
営業案件 3 取引先企業 2 0
営業案件 4 取引先企業 2 250

次の例は、aggregate 属性を使用して count 集計関数を使う方法を示しています。

// *****************************************************************************************************************
//                FetchXML      opportunity_count   Aggregate 2
// *****************************************************************************************************************
// Fetch the count of all opportunities.  This is the equivalent of
// SELECT COUNT(*) AS opportunity_count ... in SQL.
string opportunity_count = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='name' alias='opportunity_count' aggregate='count'/> 
   </entity> 
</fetch>";

EntityCollection opportunity_count_result = _serviceProxy.RetrieveMultiple(new FetchExpression(opportunity_count));

foreach (var c in opportunity_count_result.Entities)
{
   Int32 aggregate2 = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate2); 

}

CountColumn

次の例は、aggregate 属性を使用して countcolumn 集計関数を使う方法を示しています。

// *****************************************************************************************************************
//                FetchXML      opportunity_colcount   Aggregate 3
// *****************************************************************************************************************
// Fetch the count of all opportunities.  This is the equivalent of 
// SELECT COUNT(name) AS opportunity_count ... in SQL.
string opportunity_colcount = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='name' alias='opportunity_colcount' aggregate='countcolumn'/> 
   </entity> 
</fetch>";

EntityCollection opportunity_colcount_result = _serviceProxy.RetrieveMultiple(new FetchExpression(opportunity_colcount));

foreach (var c in opportunity_colcount_result.Entities)
{
   Int32 aggregate3 = (Int32)((AliasedValue)c["opportunity_colcount"]).Value;
   System.Console.WriteLine("Column count of all opportunities: " + aggregate3);

}

個別の列のカウント

次の例は、aggregate 属性を countcolumn 集計関数と distinct 属性と使って、それぞれの列をカウントする方法を示しています。

// *****************************************************************************************************************
//                FetchXML      opportunity_distcount   Aggregate 4
// *****************************************************************************************************************
// Fetch the count of distinct names for opportunities.  This is the equivalent of 
// SELECT COUNT(DISTINCT name) AS opportunity_count ... in SQL.
string opportunity_distcount = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='name' alias='opportunity_distcount' aggregate='countcolumn' distinct='true'/> 
   </entity> 
</fetch>";

EntityCollection opportunity_distcount_result = _serviceProxy.RetrieveMultiple(new FetchExpression(opportunity_distcount));

foreach (var c in opportunity_distcount_result.Entities)
{
   Int32 aggregate4 = (Int32)((AliasedValue)c["opportunity_distcount"]).Value;
   System.Console.WriteLine("Distinct name count of all opportunities: " + aggregate4);

}

Max

Dataverse でデータの最大値を計算する場合、Null 値は考慮されません。 ただし、ゼロ (0) は使用されます。

次の例は、aggregate 属性を使用して max 集計関数を使う方法を示しています。

// *****************************************************************************************************************
//                FetchXML      estimatedvalue_max   Aggregate 5
// *****************************************************************************************************************
// Fetch the maximum estimatedvalue of all opportunities.  This is the equivalent of 
// SELECT MAX(estimatedvalue) AS estimatedvalue_max ... in SQL.
string estimatedvalue_max = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
       <attribute name='estimatedvalue' alias='estimatedvalue_max' aggregate='max' /> 
   </entity> 
</fetch>";

EntityCollection estimatedvalue_max_result = service.RetrieveMultiple(new FetchExpression(estimatedvalue_max));

foreach (var c in estimatedvalue_max_result.Entities)
{
   decimal aggregate5 = ((Money)((AliasedValue)c["estimatedvalue_max"]).Value).Value;
   Console.WriteLine("Max estimated value of all opportunities: " + aggregate5);

}

Min

Dataverse でデータの最小値を計算する場合、Null 値は考慮されません。 ただし、ゼロ (0) は使用されます。

次の例は、aggregate 属性を使用して min 集計関数を使う方法を示しています。

// *****************************************************************************************************************
//                FetchXML      estimatedvalue_min   Aggregate 6
// *****************************************************************************************************************
// Fetch the minimum estimatedvalue of all opportunities.  This is the equivalent of 
// SELECT MIN(estimatedvalue) AS estimatedvalue_min ... in SQL.
string estimatedvalue_min = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='estimatedvalue' alias='estimatedvalue_min' aggregate='min' /> 
   </entity> 
</fetch>";

EntityCollection estimatedvalue_min_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_min));

foreach (var c in estimatedvalue_min_result.Entities)
{
   decimal aggregate6 = ((Money)((AliasedValue)c["estimatedvalue_min"]).Value).Value;
   System.Console.WriteLine("Minimum estimated value of all opportunities: " + aggregate6);

}

Sum

次の例は、aggregate 属性を使用して sum 集計関数を使う方法を示しています。

// *****************************************************************************************************************
//                FetchXML      estimatedvalue_sum   Aggregate 7
// *****************************************************************************************************************
// Fetch the sum of estimatedvalue for all opportunities.  This is the equivalent of 
// SELECT SUM(estimatedvalue) AS estimatedvalue_sum ... in SQL.
string estimatedvalue_sum = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum' /> 
   </entity> 
</fetch>";

EntityCollection estimatedvalue_sum_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_sum));

foreach (var c in estimatedvalue_sum_result.Entities)
{
   decimal aggregate7 = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate7);

}

複数の集計

次の例は、複数の aggregate 属性を使用して、単一の要求で countsum、および avg 集計関数を使用する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      estimatedvalue_avg, estimatedvalue_sum   Aggregate 8
// *****************************************************************************************************************
// Fetch multiple aggregate values within a single query.
string estimatedvalue_avg2 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
   </entity> 
</fetch>";

EntityCollection estimatedvalue_avg2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_avg2));

foreach (var c in estimatedvalue_avg2_result.Entities)
{
   Int32 aggregate8a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate8a);
   decimal aggregate8b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate8b);
   decimal aggregate8c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate8c);

}

グループ化の基準

次の例は、複数の aggregate 属性およびリンクされた groupby 属性の使用方法を示しています。

// *****************************************************************************************************************
//                FetchXML      groupby1   Aggregate 9
// *****************************************************************************************************************
// Fetch a list of users with a count of all the opportunities they own using groupby.
string groupby1 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='name' alias='opportunity_count' aggregate='countcolumn' /> 
      <attribute name='ownerid' alias='ownerid' groupby='true' /> 
   </entity> 
</fetch>";

EntityCollection groupby1_result = _serviceProxy.RetrieveMultiple(new FetchExpression(groupby1));

foreach (var c in groupby1_result.Entities)
{
   Int32 aggregate9a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate9a + "\n");
   string aggregate9b = ((EntityReference)((AliasedValue)c["ownerid"]).Value).Name;
   System.Console.WriteLine("Owner: " + aggregate9b);
   string aggregate9c = (string)((AliasedValue)c["ownerid_owneridyominame"]).Value;
   System.Console.WriteLine("Owner: " + aggregate9c);
   string aggregate9d = (string)((AliasedValue)c["ownerid_owneridyominame"]).Value;
   System.Console.WriteLine("Owner: " + aggregate9d);
}

以降の例では、次のグループ化の例を示します。

リンクしているエンティティによるグループ化

次の例は、aggregate 属性を使用して sum 集計関数をリンクされたテーブル値に適用する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      groupby2   Aggregate 10
// *****************************************************************************************************************
// Fetch the number of opportunities each manager's direct reports 
// own using a groupby within a link-entity.
string groupby2 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='name' alias='opportunity_count' aggregate='countcolumn' /> 
      <link-entity name='systemuser' from='systemuserid' to='ownerid'>
          <attribute name='parentsystemuserid' alias='managerid' groupby='true' />
      </link-entity> 
   </entity> 
</fetch>";

EntityCollection groupby2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(groupby2));

foreach (var c in groupby2_result.Entities)
{

     int? aggregate10a = (int?)((AliasedValue)c["opportunity_count"]).Value;
     System.Console.WriteLine("Count of all opportunities: " + aggregate10a + "\n");
}

年別のグループ化

日付に対するグループ化では、日、週、月、四半期、または年の値を使用します。 次の例は、aggregate 属性と groupby 属性を使用して、結果を年別にグループ化する方法を示しています。


// *****************************************************************************************************************
//                FetchXML      byyear   Aggregate 11           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by year.
string byyear = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
      <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />
      <filter type='and'>
          <condition attribute='statecode' operator='eq' value='Won' />
      </filter>
   </entity> 
</fetch>";

EntityCollection byyear_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyear));

foreach (var c in byyear_result.Entities)
{
   Int32 aggregate11 = (Int32)((AliasedValue)c["year"]).Value;
   System.Console.WriteLine("Year: " + aggregate11);                      
   Int32 aggregate11a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate11a);
   decimal aggregate11b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate11b);
   decimal aggregate11c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate11c);
   System.Console.WriteLine("----------------------------------------------");
}

四半期別のグループ化

次の例は、aggregate 属性と groupby 属性を使用して、結果を四半期別にグループ化する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      byquarter   Aggregate 12           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by quarter.(returns 1-4)
string byquarter = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='opportunity'> 
       <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
       <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
       <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />
       <filter type='and'>
           <condition attribute='statecode' operator='eq' value='Won' />
       </filter>
    </entity> 
</fetch>";

EntityCollection byquarter_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byquarter));

foreach (var c in byquarter_result.Entities)
{
    Int32 aggregate12 = (Int32)((AliasedValue)c["quarter"]).Value;
    System.Console.WriteLine("Quarter: " + aggregate12);
    Int32 aggregate12a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
    System.Console.WriteLine("Count of all opportunities: " + aggregate12a);
    decimal aggregate12b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
    System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate12b);
    decimal aggregate12c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
    System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate12c);
    System.Console.WriteLine("----------------------------------------------");
}

月別のグループ化

次の例は、aggregate 属性と groupby 属性を使用して、結果を月別にグループ化する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      bymonth   Aggregate 13           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by month. (returns 1-12)
string bymonth = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='opportunity'> 
       <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
       <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
       <attribute name='actualclosedate' groupby='true' dategrouping='month' alias='month' />
       <filter type='and'>
           <condition attribute='statecode' operator='eq' value='Won' />
       </filter>
    </entity> 
</fetch>";

EntityCollection bymonth_result = _serviceProxy.RetrieveMultiple(new FetchExpression(bymonth));

foreach (var c in bymonth_result.Entities)
{
    Int32 aggregate13 = (Int32)((AliasedValue)c["month"]).Value;
    System.Console.WriteLine("Month: " + aggregate13);
    Int32 aggregate13a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
    System.Console.WriteLine("Count of all opportunities: " + aggregate13a);
    decimal aggregate13b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
    System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate13b);
    decimal aggregate13c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
    System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate13c);
    System.Console.WriteLine("----------------------------------------------");
}

週別のグループ化

次の例は、aggregate 属性と groupby 属性を使用して、結果を週別にグループ化する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      byweek   Aggregate 14           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by week. (Returns 1-52)
string byweek = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
      <attribute name='actualclosedate' groupby='true' dategrouping='week' alias='week' />
      <filter type='and'>
          <condition attribute='statecode' operator='eq' value='Won' />
      </filter>
   </entity> 
</fetch>";

EntityCollection byweek_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byweek));

foreach (var c in byweek_result.Entities)
{
   Int32 aggregate14 = (Int32)((AliasedValue)c["week"]).Value;
   System.Console.WriteLine("Week: " + aggregate14);
   Int32 aggregate14a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate14a);
   decimal aggregate14b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate14b);
   decimal aggregate14c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate14c);
   System.Console.WriteLine("----------------------------------------------");
}

日別のグループ化

次の例は、aggregate 属性と groupby 属性を使用して、結果を日別にグループ化する方法を示しています。

// *****************************************************************************************************************
//                FetchXML      byday   Aggregate 15           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by day. (Returns 1-31)
string byday = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
      <attribute name='actualclosedate' groupby='true' dategrouping='day' alias='day' />
      <filter type='and'>
          <condition attribute='statecode' operator='eq' value='Won' />
      </filter>
   </entity> 
</fetch>";

EntityCollection byday_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byday));

foreach (var c in byday_result.Entities)
{
   Int32 aggregate15 = (Int32)((AliasedValue)c["day"]).Value;
   System.Console.WriteLine("Day: " + aggregate15);
   Int32 aggregate15a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate15a);
   decimal aggregate15b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate15b);
   decimal aggregate15c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate15c);
   System.Console.WriteLine("----------------------------------------------");
}

複数のグループ化

次の例は、aggregate 属性および複数の groupby 句の使用方法を示しています。

// *****************************************************************************************************************
//                FetchXML      byyrqtr   Aggregate 16           
// *****************************************************************************************************************
// Fetch aggregate information about the opportunities that have 
// been won by year and quarter.
string byyrqtr = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
      <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />
      <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />
      <filter type='and'>
          <condition attribute='statecode' operator='eq' value='Won' />
      </filter>
   </entity> 
</fetch>";

EntityCollection byyrqtr_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyrqtr));

foreach (var c in byyrqtr_result.Entities)
{
   Int32 aggregate16d = (Int32)((AliasedValue)c["year"]).Value;
   System.Console.WriteLine("Year: " + aggregate16d);
   Int32 aggregate16 = (Int32)((AliasedValue)c["quarter"]).Value;
   System.Console.WriteLine("Quarter: " + aggregate16);
   Int32 aggregate16a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate16a);
   decimal aggregate16b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate16b);
   decimal aggregate16c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate16c);
   System.Console.WriteLine("----------------------------------------------");
}

並べ替え

次の例は、aggregate 属性および複数の orderby 句の使用方法を示しています。

// *****************************************************************************************************************
//                FetchXML      byyrqtr2   Aggregate 17           
// *****************************************************************************************************************
// Specify the result order for the previous sample.  Order by year, then quarter.
string byyrqtr2 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
   <entity name='opportunity'> 
      <attribute name='opportunityid' alias='opportunity_count' aggregate='count'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 
      <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/> 
      <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />
      <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />
      <order alias='year' descending='false' />
      <order alias='quarter' descending='false' />
      <filter type='and'>
          <condition attribute='statecode' operator='eq' value='Won' />
      </filter>
   </entity> 
</fetch>";

EntityCollection byyrqtr2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyrqtr2));

foreach (var c in byyrqtr2_result.Entities)
{
   Int32 aggregate17 = (Int32)((AliasedValue)c["quarter"]).Value;
   System.Console.WriteLine("Quarter: " + aggregate17);
   Int32 aggregate17d = (Int32)((AliasedValue)c["year"]).Value;
   System.Console.WriteLine("Year: " + aggregate17d);
   Int32 aggregate17a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
   System.Console.WriteLine("Count of all opportunities: " + aggregate17a);
   decimal aggregate17b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
   System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate17b);
   decimal aggregate17c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
   System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate17c);
   System.Console.WriteLine("----------------------------------------------");
}

参照

FetchXML による大量の結果セットのページング
Fetch XML スキーマ
RetrieveMultiple
RetrieveMultipleRequest
FetchExpression

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。