首页 > 其他分享 >Dynamics CRM 365 使用 FetchXml 聚合数据(Aggregate data using FetchXml)

Dynamics CRM 365 使用 FetchXml 聚合数据(Aggregate data using FetchXml)

时间:2024-06-17 09:55:26浏览次数:11  
标签:13 聚合 示例 52 FetchXml -----------------------------------------------------------

前言

FetchXML 包括分组和聚合功能,可用于计算多行数据的总和、平均值、最小值、最大值和计数。

若要返回聚合值,必须:

  • aggregate设置为true。

  • 为每个属性元素设置别名alias 属性。

  • 将每个属性元素的aggregate属性设置为以下聚合函数之一:

    函数 返回值
    avg 包含数据的列值的平均值。
    count 总行数
    countcolumn 该列中包含数据的行数。
    max 该列中行的最大值。
    min 该列中行的最小值。
    sum 包含数据的列值的总值。

    请注意以下几点:

    • 计算聚合值时不考虑 Null 值。
    • 使用通过 link-entity 元素联接的表中的数据。
    • 与任何查询一样,应用 filters 来限制结果。

数据聚合(Aggregate )

假设有 10 条包含以下数据的客户记录:

 -----------------------------------------------------------------------------
 | name                 | createdon      | address1_city | numberofemployees |
 -----------------------------------------------------------------------------
 | 第四极咖啡屋 (示例)     | 2024/5/9 13:52 | 北票            | 9,500             |
 -----------------------------------------------------------------------------
 | 立特威公司 (示例)       | 2024/5/9 13:52 | 厦门            | 6,000             |
 -----------------------------------------------------------------------------
 | Adventure Works (示例) | 2024/5/9 13:52 | 西丰            | 4,300             |
 -----------------------------------------------------------------------------
 | Fabrikam, Inc. (示例)  | 2024/5/9 13:52 | 新民            | 2,700             |
 -----------------------------------------------------------------------------
 | 蓝天航空公司 (示例)     | 2024/5/9 13:52 | 曲阜            | 2,900             |
 -----------------------------------------------------------------------------
 | 城市电力照明公司 (示例)  | 2024/5/9 13:52 | 大连            | 2,900             |
 -----------------------------------------------------------------------------
 | Contoso 制药公司 (示例) | 2024/5/9 13:52 | 大连            | 1,500             |
 -----------------------------------------------------------------------------
 | 阿尔卑斯山滑雪馆舍 (示例)| 2024/5/9 13:52 | 邯郸            | 4,800             |
 -----------------------------------------------------------------------------
 | A. Datum 公司 (示例)    | 2024/5/9 13:52 | 大连            | 6,200             |
 -----------------------------------------------------------------------------
 | 佳酿酒庄 (示例)         | 2024/5/9 13:52 | 新民            | 3,900             |
 -----------------------------------------------------------------------------

以下查询返回列numberofemployees的聚合数据。

string fetchXml = @"<fetch aggregate='true'>
                           <entity name='account'>
                             <attribute name='numberofemployees' alias='Average' aggregate='avg' />
                             <attribute name='numberofemployees' alias='Count' aggregate='count' />
                             <attribute name='numberofemployees' alias='ColumnCount' aggregate='countcolumn' />
                             <attribute name='numberofemployees' alias='Maximum' aggregate='max' />
                             <attribute name='numberofemployees' alias='Minimum' aggregate='min' />
                             <attribute name='numberofemployees' alias='Sum' aggregate='sum' />
                           </entity>
                         </fetch>";
FetchExpression fetchExpression = new FetchExpression(fetchXml);

//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);

返回结果集合

 --------------------------------------------------------------
 | Average | Count | ColumnCount | Maximum | Minimum | Sum    |
 --------------------------------------------------------------
 | 4,470   | 10    | 10          | 9,500   | 1,500   | 44,700 |
 --------------------------------------------------------------

非重复列值(Distinct)

使用countcolumn聚合函数,可以设置distinct属性以返回列的唯一值的计数。

string fetchXml = @"<fetch aggregate='true'>
                           <entity name='account'>
                             <attribute name='numberofemployees' alias='Average' aggregate='avg' />
                             <attribute name='numberofemployees' alias='Count' aggregate='count' />
                             <attribute name='numberofemployees' alias='ColumnCount' aggregate='countcolumn'  distinct='true' />
                             <attribute name='numberofemployees' alias='Maximum' aggregate='max' />
                             <attribute name='numberofemployees' alias='Minimum' aggregate='min' />
                             <attribute name='numberofemployees' alias='Sum' aggregate='sum' />
                           </entity>
                         </fetch>";
FetchExpression fetchExpression = new FetchExpression(fetchXml);

//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);

当为前一个查询设置时,结果返回9而不是10,因为数据集中有两行的雇员人数值为2900。

 --------------------------------------------------------------
 | Average | Count | ColumnCount | Maximum | Minimum | Sum    |
 --------------------------------------------------------------
 | 4,470   | 10    | 9           | 9,500   | 1,500   | 44,700 |
 --------------------------------------------------------------

分组 (Grouping)

通过添加带有groupby属性而不是聚合属性的属性元素,对聚合查询的结果进行分组。在分组时,应该指定一个order元素,并将别名值设置为组的别名。

例如,下面的查询返回雇员总数,并按城市计数:

string fetchXml = @"<fetch aggregate='true'>
                               <entity name='account'>
                                  <attribute name='numberofemployees' alias='Total' aggregate='sum' />
                                  <attribute name='address1_city' alias='Count' aggregate='count' />
                                  <attribute name='address1_city' alias='City' groupby='true' />
                                  <order alias='City' />
                               </entity>
                            </fetch>";
FetchExpression fetchExpression = new FetchExpression(fetchXml);

//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);

返回结果值如下:

 -------------------------
 | Total  | Count | City |
 -------------------------
 | 9,500  | 1     | 北票   |
 -------------------------
 | 10,600 | 3     | 大连   |
 -------------------------
 | 4,800  | 1     | 邯郸   |
 -------------------------
 | 2,900  | 1     | 曲阜   |
 -------------------------
 | 4,300  | 1     | 西丰   |
 -------------------------
 | 6,000  | 1     | 厦门   |
 -------------------------
 | 6,600  | 2     | 新民   |
 -------------------------

按日期的各个部分分组 (dategrouping)

Value 描述
day 按月份中的某一天分组
week 按一年中的一周分组
month 按一年中的月份分组
quarter 按财政年度季度分组
year 按年份分组
fiscal-period 按会计年度期间分组
fiscal-year 按会计年度分组

以下查询按记录创建时间对显示员工数的客户记录进行分组:

string fetchXml = @"<fetch aggregate='true'>
            <entity name='account'>
            <attribute name='numberofemployees' alias='Total' aggregate='sum' />
            <attribute name='createdon' alias='Day' groupby='true' dategrouping='day' />
            <attribute name='createdon' alias='Week' groupby='true' dategrouping='week' />
            <attribute name='createdon' alias='Month' groupby='true' dategrouping='month' />
            <attribute name='createdon' alias='Year' groupby='true'  dategrouping='year' />
            <attribute name='createdon' alias='FiscalPeriod' groupby='true' dategrouping='fiscal-period' />
            <attribute name='createdon' alias='FiscalYear' groupby='true' dategrouping='fiscal-year' />
            <order alias='Month' />
            </entity>
         </fetch>";
FetchExpression fetchExpression = new FetchExpression(fetchXml1);

//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);

返回结果

 -------------------------------------------------------------------
 | Total  | Day | Week | Month | Year  | FiscalPeriod | FiscalYear |
 -------------------------------------------------------------------
 | 44,700 | 9   | 19   | 5     | 2,024 | FY20242 季度   | FY2024     |
 -------------------------------------------------------------------

行聚合(Row aggregate)

当表具有定义了层次结构关系后,可以在“查找”列上返回层次结构关系的行聚合。

当子帐户记录的parentaccountid列等于当前帐户的accountid列时,下面的示例返回名为CountChildren的列中相关帐户的数量。

<fetch>
   <entity name='account'>
      <attribute name='name' />
      <attribute name='accountid' alias='numberOfChildren' rowaggregate='CountChildren' />
      <order attribute='accountid' descending='true' />
   </entity>
</fetch>

局限性

返回聚合值的查询限制为 50,000 条记录。此限制有助于保持系统性能和可靠性。如果查询中的筛选条件返回超过 50,000 条记录,则会收到以下错误:

Number: -2147164125
Code: 8004E023
Message: AggregateQueryRecordLimit exceeded. Cannot perform this operation.
Client error message: The maximum record limit is exceeded. Reduce the number of records.

若要避免此错误,请向查询添加适当的筛选器,以确保查询的评估值不超过 50,000 条记录。然后多次运行查询并合并结果。适当的筛选器取决于数据的性质,但它们可以是日期范围或选项列中的值子集。

Per query limit

即使应用了聚合查询的默认限制,查询也可能需要一些时间才能完成。您可以在查询中使用aggregatelimit属性来应用自定义下限,如果结果高于自定义限制,则返回AggregateQueryRecordLimit exceeded错误。

在此示例中,自定义最大行数限制为 10:

<fetch aggregate='true'
   aggregatelimit = '10'>
   <entity name='opportunity'>
      <attribute name='name'
         alias='opportunity_count'
         aggregate='count' />
   </entity>
</fetch>

The per query limit 不能超过默认聚合限制。

标签:13,聚合,示例,52,FetchXml,-----------------------------------------------------------
From: https://www.cnblogs.com/YuYangBlogs/p/18247712

相关文章

  • DataGridView列填充实体类
    使用的地方:假如你有一个名为rgvProcessDtl的DataGridView控件DataTabledt=(DataTable)rgvProcessDtl.DataSource;foreach(DataRowrowindt){OG_ProcessGuidDtldtl=newOG_ProcessGuidDtl();FillModel(dtl,row);//将实体类对象与grid行填充}//////将数据填入实......
  • ETL可视化工具 DataX -- 简介( 一)
    引言DataX系列文章:ETL可视化工具DataX–安装部署(二)1.1DataX1.1.1DataX概览DataX是阿里云DataWorks数据集成的开源版本,在阿里巴巴集团内被广泛使用的离线数据同步工具/平台。DataX实现了包括MySQL、Oracle、OceanBase、SqlServer、Postgre、HDFS、Hive、A......
  • (pdf)数据结构与算法分析 Java语言描述=Data Structures and Algorithm Analysis in Jav
    书:pan.baidu.com/s/1tGbGhhQ3Ez1SIkqdEREsjQ?pwd=eqp0提取码:eqp0数组:作为最基本的数据结构,用于存储固定大小的同类型元素集合。链表:动态数据结构,允许在任意位置插入和删除元素。栈:后进先出(LIFO)的数据结构,常用于函数调用和表达式求值。队列:先进先出(FIFO)的数据结构,常用于任务调......
  • unitycatalog datagrics 开源的data&ai 多模catalog
    unitycatalogdatagrics开源的data&ai多模catalog包含的特性支持任意格式、引擎、资产的多摸接口 支持包含了deltalake,iceberg,uniform,paquert,csv。。。等格式,超越表,支持非结构化数据以及ai资产,插件化的架构,可以支持hms以及icebergrestcatalog以及其他插件(比如ai),与delt......
  • 一家令人艳羡的大数据AI公司!Databricks
    今日介绍一家大数据AI超级独角兽公司,以及它如何与当前生物基因组学相结合。它就是Databricks,没错,俗称“砖厂”。Databricks简介Databricks公司诞生于2013年,是属于Spark的商业化公司,创始人来自ApacheSpark大数据处理系统的创始团队,包括加州大学伯克利分校的AMP实验室。Databric......
  • WPF Path Data PathGeometry PathFigure Segments BezierSegment,LineSegment,ArcSeg
     BezierSegment//BezierCurveusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documen......
  • COMP20008 - Elements of Data Processing
    COMP20008- Elements of Data Processing, Semester 1,2024Assignment2–Whoelse likesthis book?1. OverviewIn thisproject, you willundertake an analysis of a collection of datasets containing detailed informationaboutbooksandt......
  • pandas ---- pd.DataFrame基本用法
    文章目录前言1loc和iloc注意事项。(后面这些都会在笔记中提到)2DataFrame的维度一、DataFrame的创建---pd.DataFrame(data,index=None,columns=None)1字典创建DataFrame(字典转Dataframe很常用)2用numpy数组或者嵌套list创建DataFrame二、DataFrame的......
  • solidity calldata学习
    在Solidity中,calldata是一种数据位置标识符,用于指定函数参数的存储位置。calldata特别适用于函数的外部调用参数,并且是只读的。以下是对Solidity中数据位置的一些说明:storage:用于状态变量,数据持久存储在区块链上。修改状态变量会消耗gas。memory:用于临时变量,这些变量......
  • C# 对DataTable 的某一列进行去重,并保留其他列,其他列不去重(转)
    可以使用LINQ来对DataTable的某一列进行去重操作,并同时保留其他列。以下是一个示例代码:usingSystem;usingSystem.Data;usingSystem.Linq;classProgram{staticvoidMain(){//创建一个示例的DataTableDataTabletable=newDataTable();......