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

 * 创建日期: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. cocos2d-x 调用第三方so文件

    一:假设.so文件名称 : libhi.so 1.jni文件下创建一个prebuilt 2.android.mk文件中找到  include $(CLEAR_VARS), 在这句后面添加如下代码 in ...

  2. jsp: ServletContext

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用. ServletConfig对象中维护了ServletContext对象的引用,开发 ...

  3. Laravel5.1学习笔记20 EloquentORM 关系

    Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...

  4. Python之IPython开发实践

    Python之IPython开发实践 1. IPython有行号. 2. Tab键自动完成,当前命名空间任何与已输入字符串相匹配的变量就会被找出来. 3. 内省机制,在变量前或者后面加上(?)问号,就 ...

  5. 1B课程笔记分享_StudyJams_2017

    课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...

  6. 重现apache commons fileupload DOS漏洞

    这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资 ...

  7. JS——“==”与“===”

    ==: 两个等于号只是比较两个变量的值 var n1 = 1; var n2 = "1"; alert(n1 == n2);//返回true ===: 三个等于号不仅比较值而且比较 ...

  8. win10 打开chm文件内容空白如何解决

    win10 打开chm文件内容空白如何解决 .CHM文件是非常常见的帮助文件格式.由于其便携性,很多小说或杂志也会采用chm格式.win7/win8.1/win10系统,由于采用了UAC,致使原本在x ...

  9. RadioButtonList的兩種實現方式

    一種是修改ItemTemplate,即ListBoxItem裏面的内容 <ListBox ItemsSource="{Binding}"> <ListBox.It ...

  10. Learning opencv续不足(七)线图像的设计D

    因为线图像startline有了起点和终点,我们就可以用DDA法求出线上所有点,任意斜率直线通过四象限八区域查表法界定.我们只示范一个区域:函数为: public PointF DdaFindPtIm ...