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

 * 创建日期: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. EasyUI系列学习(二)-使用EasyUI

    一.引入必要文件 <!--1.0引入jQuery核心库--> <script src="jquery-easyui-1.4.4/jquery.min.js"> ...

  2. MVC系列学习(一)-新语法

    本篇内容: 1.自动属性 2.隐式类型 3.对象初始化器和集合初始化器 4.匿名类型 5.扩展方法 6.Lambda表达式 1.自动属性 使用: class Student { public stri ...

  3. SQL Server应用模式之OLTP系统性能分析

    OLTP系统的最大特点,是这类应用里有大量的,并发程度比较高的小事务,包括SELECT.INSERT.UPDATE和DELETE. 这些操作都比较简单,事务时间也不会很长,但是要求的返回时间很严格,基 ...

  4. web流行工具

    中小型公司: Node.js:现代工业化前端的基础: RequireJS:AMD规范, 即将过时的 JavaScript 模块化方案: Bower:前端模块源: npm:前端工具源,另一个潜在的前端模 ...

  5. Go 时间相关

    >获取当前时间: t := time.Now() >获取当天开始.结束时间: tm1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, ...

  6. HDU_1063_Exponentiation_大数

    Exponentiation Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. ipc (进程间通信

    进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法.进程是计算机系统分配资源的最小单位(严格说来是线程).每个进程都有自 ...

  8. CDR X8图框精确剪裁在哪?

    对于CorelDRAW,刚从低版本升级为高版本的同学可能一下子理不清方向,因为在CorelDRAW X8中有很多功能命令做了整改和位置的变化.很多用户反映,CDR中的图框精确剪裁不见了,然而并不是该命 ...

  9. js中关于new Object时传参的一些细节分析

    1, 参数是一个对象,核心js对象(native ECMAScript object)或宿主对象(host object),那么将直接返回该对象. 其生成的对象构造器仍然是所传参数对象的构造器.这样造 ...

  10. 名词解释http隧道、https、SSL层、http代理、在线代理、socks代理区别

    以前听到这几个名词时,总是搞混淆,今天花点时间来记录这几个名词的大概区别,方便以后自己查看. http隧道与https http隧道:“HTTP隧道技术”就是把所有要传送的数据全部封装到HTTP协议里 ...