/* 创建者:菜刀居士的博客

 * 创建日期:2014年07月08号

 */

namespace Net.CRM.FetchXml

{

    using System;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

/// <summary>

    /// 使用FetchXml聚合查询

    /// </summary>

    public class FetchXmlDemo

    {

        /* 特别提示:FetchXML 包含使您可以计算总和、平均值、最小值、最大值和计数的分组和聚合函数。

         * 在查询中仅仅能指定一个 aggregate 属性,并且不能使用 distinct keyword。要创建的聚合的属性。

         * 请设置keywordaggregate到true,然后指定有效的实体名称。 属性名称,和别名 (变量名)。

         * 同一时候必须指定要运行的聚合的类型。 

         */

/// <summary>

        /// 总和

        /// sql: select sum(new_value) as 'new_value_sum' from account

        /// </summary>

        public void Sum(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_value' alias='new_value_sum' aggregate='sum' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

               Entity en = ec.Entities[0];

               //获取结果

               decimal value = ((Money)((AliasedValue)en["new_value_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 平均值

        /// sql: select avg(new_value) as 'new_value_avg' from account

        /// 当crm计算数据的平均值时,不考虑 Null 值。

可是。会使用零 (0)。

        /// </summary>

        public void Avg(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_value' alias='new_value_avg' aggregate='avg' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                decimal value = ((Money)((AliasedValue)en["new_value_avg"]).Value).Value;

            }

        }

/// <summary>

        /// 计算有多少个记录

        /// sql: select count(*) from account

        /// </summary>

        public void Count(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_name' alias='new_name_count' aggregate='count' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value = (Int32)((AliasedValue)en["new_name_count"]).Value;

            }

        }

/// <summary>

        /// 计算有多少个记录(针对指定的列名)

        /// sql: select count(distinct new_name) from account

        /// </summary>

        public void CountColumn(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_name' alias='new_name_count' aggregate='countcolumn' distinct='true' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value = (Int32)((AliasedValue)en["new_name_count"]).Value;

            }

        }

/// <summary>

        /// 最大值

        /// sql: select max(new_value) as 'new_value_max' from account

        /// 当crm计算数据的最大值时,不考虑 Null 值。可是,会使用零 (0)。

        /// </summary>

        public void Max(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_value' alias='new_value_max' aggregate='max' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                decimal value = ((Money)((AliasedValue)en["new_value_max"]).Value).Value;

            }

        }

/// <summary>

        /// 最小值

        /// sql: select min(new_value) as 'new_value_min' from account

        /// 当crm计算数据的最小值时,不考虑 Null 值。可是。会使用零 (0)。

        /// </summary>

        public void Min(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_value' alias='new_value_min' aggregate='min' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                decimal value = ((Money)((AliasedValue)en["new_value_min"]).Value).Value;

            }

        }

/// <summary>

        /// 多个聚合

        /// sql: select count(*) as 'new_value_count',max(new_value) as 'new_value_max',

        ///       min(new_value) as 'new_value_min' from account

        /// </summary>

        public void CountAndMaxAndMin(IOrganizationService service)

        {

            string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>


                                    <entity name='account'>

                                        <attribute name='new_value' alias='new_value_count' aggregate='count' />

                                        <attribute name='new_value' alias='new_value_max' aggregate='max' />

                                        <attribute name='new_value' alias='new_value_min' aggregate='min' />


                                    </entity>

                                </fetch>";

            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (ec != null && ec.Entities.Count > 0)

            {

                Entity en = ec.Entities[0];

                //获取结果

                int count_value = (Int32)((AliasedValue)en["new_value_count"]).Value;

                decimal max_value = ((Money)((AliasedValue)en["new_value_max"]).Value).Value;

                decimal min_value = ((Money)((AliasedValue)en["new_value_min"]).Value).Value;

            }

        }

    }

}

crm使用FetchXml聚合查询的更多相关文章

  1. crm使用FetchXml分组聚合查询

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月09号  */ namespace Net.CRM.FetchXml {     using System;     using Micr ...

  2. Dynamics CRM 2015/2016 Web API:聚合查询

    各位小伙伴们,今天是博主2016年发的第一篇文章.首先祝大家新年快乐.工资Double,哈哈.今天我们来看一个比較重要的Feature--使用Web API运行FetchXML查询! 对的,各位.你们 ...

  3. python操作mongodb之二聚合查询

    #聚合查询 from pymongo import MongoClient db = MongoClient('mongodb://10.0.0.9:27017/').aggregation_exam ...

  4. [SQL基础教程] 3-1 对表进行聚合查询

    [SQL基础教程] 3-1 对表进行聚合查询 聚合函数 用于合计的函数称为聚合函数或者集合函数 COUNT SUM AVG MAX MIN SELECT COUNT(*) FROM table; SE ...

  5. 开发中使用mongoTemplate进行Aggregation聚合查询

    笔记:使用mongo聚合查询(一开始根本没接触过mongo,一点一点慢慢的查资料完成了工作需求) 需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话.地址.支付总金额以及总 ...

  6. mongodb高级聚合查询

    在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysql复杂很多. 注:本文基于 mo ...

  7. ThinkPHP 数据库操作(四) : 聚合查询、时间查询、高级查询

    聚合查询 在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数.所有用户的最大积分.用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括: 用法示例: ...

  8. ElasticSearch 6.2 Mapping参数说明及text类型字段聚合查询配置

    背景: 由于本人使用的是6.0以上的版本es,在使用发现很多中文博客对于mapping参数的说明已过时.ES6.0以后有很多参数变化. 现我根据官网总结mapping最新的参数,希望能对大家有用处. ...

  9. orm分组,聚合查询,执行原生sql语句

    from django.db.models import Avg from app01 import models annotate:(聚合查询) ret=models.Article.objects ...

随机推荐

  1. E - Fibonacci Again(找规律)

    逐渐发现找规律的美妙之处啦,真不错,用普通方法解决很久或者很麻烦的问题,找到规律就很方便,算法最主要还是思想 Description There are another kind of Fibonac ...

  2. 由zImage生成uImage

    一.手动使用mkimage命令 mkimage -A arm -O linux -T kernel -C none -a 30007fc0 -e 30007fc0 -n uImage   -d /wo ...

  3. PHPer的等级划分

    PHPer的等级划分 前一段时间刚刚完成PHP的培训,然后想知道自己目前的水平(或者说等级),并且应该在哪些方面进行提高,所以在网上查了一下相关介绍.其中有一篇介绍讲的挺清楚的,至少目前的我还是很认同 ...

  4. Visual Studio 2015编译安装配置QT5.5.1(含QTWEBKIT)

    尽管QT5.5.1和VisualStudio 2015都已经发布很久了,但是QT项目组视乎不会为QT5.5.1专门发布预编译的QT5.5.1 for windows(2015)版本的,也不会专门发布V ...

  5. 数论(容斥原理)hdu-4509-The Boss on Mars

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个n,求1~n中与n互质的数的4次方的总和. 解题思路: 容斥原理.逆元.公式 ...

  6. Linux c c++ 开发调试技巧

    看到一篇介绍 linux c/c++ 开发调试技巧的文章,感觉挺使用,哪来和大家分享. 通向 UNIX 天堂的 10 个阶梯Author: Arpan Sen, 高级技术人员, Systems Doc ...

  7. poj1887 Testing the CATCHER

    Testing the CATCHER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13968   Accepted: 5 ...

  8. 删除: warning C4996: &#39;sprintf&#39;: This function or variable may be unsafe. Consider 方法

    可以使用的最简单的方法: 选项Project   |   Configuration   Properties   |   C/C++   |   Preprocessor   |   Preproc ...

  9. FineReport实现Java报表主题分析的效果图

    Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...

  10. cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062: 解决办法

    问题原因:很可能是/var/log的权限设置不正确.首先执行 mkpasswd 和 mkgroup 重新生成权限信息,再删除sshd服务,重新配置 解决办法: $ mkpasswd -l > / ...