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

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

 */

namespace Net.CRM.FetchXml

{

    using System;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

/// <summary>

    /// 使用FetchXml聚合查询,分组根据

    /// </summary>

    public class FetchXmlExtension

    {

        /// <summary>

        /// 分组聚合

        /// sql: select count(*),ownerid from account group by ownerid

        /// </summary>

        public void Group(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                        <attribute name='name' alias='name_count' aggregate='count' />


                                        <attribute name='ownerid' alias='ownerid' groupby='true' />


                                    </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["name_count"]).Value).Value;

                EntityReference ownerEr = (EntityReference)((AliasedValue)en["ownerid"]).Value;

            }

        }

/// <summary>

        /// 分组聚合,按年分组

        /// </summary>

        public void GroupByYear(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_year = (Int32)((AliasedValue)en["year"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;


            }

        }

/// <summary>

        /// 分组聚合,按季度分组

        /// </summary>

        public void GroupByQuarter(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_quarter = (Int32)((AliasedValue)en["quarter"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按月分组

        /// </summary>

        public void GroupByMonth(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='month' alias='month' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_month = (Int32)((AliasedValue)en["month"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按周分组

        /// </summary>

        public void GroupByWeek(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='week' alias='week' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_week = (Int32)((AliasedValue)en["week"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,按日分组

        /// </summary>

        public void GroupByDay(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='day' alias='day' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_day = (Int32)((AliasedValue)en["day"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

/// <summary>

        /// 分组聚合,多个分组根据

        /// </summary>

        public void GroupByYearAndQuarter(IOrganizationService service)

        {

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


                                    <entity name='account'>

                                       <attribute name='accountid' alias='account_count' aggregate='count'/>


                                       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/> 


                                       <attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />


                                       <attribute name='actualclosedate' groupby='true' dategrouping='quarter' alias='quarter' />


                                    </entity>

                                </fetch>";

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

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

            {

                Entity en = ec.Entities[0];

                //获取结果

                int value_year = (Int32)((AliasedValue)en["year"]).Value;

                int value_quarter = (Int32)((AliasedValue)en["quarter"]).Value;

                int value_count = (Int32)((AliasedValue)en["account_count"]).Value;

                decimal value_sum = ((Money)((AliasedValue)en["estimatedvalue_sum"]).Value).Value;

            }

        }

    }

}

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

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

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

  2. Solr分组聚合查询之Facet

    摘要: Solr的分组聚合是一个笼统的概念,目的就是把查询结果做分类,有多种方式可以做到很类似的结果.也正是由于它们的不同表现,可以适合于多种场景. 何为Facet Facet是一种手段,用来将搜索结 ...

  3. Solr分组聚合查询之Group

    摘要: Solr对结果的分组处理除了facet还可以使用group.Solr的group是根据某一字段对结果分组,将每一组内满足查询的结果按顺序返回. Group对比Facet Group和Facet ...

  4. SQL分组聚合查询练习(SQL Server和Oracle相似)20190514

    先建表 CREATE TABLE [dbo].[orderdt_jimmy]( ,) NOT NULL, [order_nid] [int] NOT NULL, ) NOT NULL, [qty] [ ...

  5. Django对postgresql数据库进行分组聚合查询

    action(methods=['GET'], detail=False, url_path='count') def count(self, request): """ ...

  6. solrcloud jsonfacet分组聚合 unique计数不准确

    jsonfacet分组聚合查询 unique.hll函数问题: 对不同的值进行估算,并非准确的值, 优点:节省内存消耗,用分组算法对不同的值count进行估算 缺点:无法准确统计count(disti ...

  7. django第10天(聚合查询,常用字段)

    django第10天 聚合查询 聚合函数的使用场景 单独使用:不分组,只查聚合结果 分组使用:按字段分组,可查分组字段与聚合结果 导入聚合函数 from django.db.models import ...

  8. 浅析MySQL使用 GROUP BY 分组聚合与细分聚合

    原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7521278.html: 若有错误,请评论指出,谢谢! 1. 聚合函数(Aggregate Function ...

  9. Flask聚合函数(基本聚合函数、分组聚合函数、去重聚合函数))

    Flask聚合函数 1.基本聚合函数(sun/count/max/min/avg) 使用聚合函数先导入:from sqlalchemy import func 使用方法: sun():func.sum ...

随机推荐

  1. hibernate annotation 生成uuid主键

    JPA标准方式下,不可以生成uuid类型的主键,但是hibernate提供了一些方式生成uuid主键,具体如下: 1.主键生成器     @GeneratedValue(generator=" ...

  2. [Codeforces]Codeforces Round #489 (Div. 2)

    Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...

  3. 慕课网中网页定位导航中js相关问题总结

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  4. node(koa)微信公众号接口认证

    使用微信测试号, 花生壳内网穿透 需要注意 token 两边都是自定义, 需要保持一致, const Koa = require('koa') const sha1 = require('sha1') ...

  5. python 分割文件、组合文件

    import glob big_file = open('index.sql', 'rb') bak_file = 'index_bak' i = 1 while True: chunk = big_ ...

  6. 自己写一个爬虫 copider

    copider 模仿scrapy的一些写法,当然我这个是单进程的,不是异步的 1.目录 copider/copider.py #coding=utf-8 ''' Created on 2015年10月 ...

  7. mysql_基础2

    创建数据表:

  8. mysql_基础1

    初学mysql,感觉挺有意思的. mysql指令的一些参数: promrt修改提示符:   PROMPT \D mysql的语法规范: 一些函数: 创建数据库: SHOW CREATE DATABAS ...

  9. SpringMVC知识点总结一(非注解方式的处理器与映射器配置方法)

    一.SpringMVC处理请求原理图(参见以前博客) 1.  用户发送请求至前端控制器DispatcherServlet 2.  DispatcherServlet收到请求调用HandlerMappi ...

  10. Ansible 利用playbook批量部署Nginx

    我这里直接部署的,环境已经搭建好,如果不知道的小伙伴可以看上一遍ansible搭建,都写好了,这里是根据前面环境部署的 192.168.30.21     ansible 192.168.30.25  ...