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

 * 创建日期: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. SQL Sever语言 事务

    事务:保障流程的完整执行保证程序某些程序在运行时同时成功同时失败,保证程序的安全性 begin tran --在流程开始的位置加 --此处写SQL语句 if @@error>0 --ERRORS ...

  2. windows server 2008 r2 安裝IE11

    https://support.microsoft.com/en-us/help/2847882/prerequisite-updates-for-internet-explorer-11 https ...

  3. D3.js 力导向图(小气泡围绕中心气泡)

    html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...

  4. Java中的异常注意点

    在java中 使用throw关键字抛出异常       使用throws关键字声明异常 public static void main(String[] args) throws Exception{ ...

  5. [转][IPC通信]基于管道的popen和pclose函数

    标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由popen启动的进程称为子进程. popen函数还创建一个管道用于父子进 ...

  6. WEB开发模式浅析

    WEB技术随着互联网的崛起而崛起,又随着移动互联网的发展而呈现更加多样化的趋势. 黑暗时代:大约在2005年以前,所谓的WEB开发主要还是美工的活,HTML/CSS占主导,Dreamwaver做为页面 ...

  7. redis集群——RPLR简笔(Redis+PostgreSQL+Linux(centos7)+RabbitMQ)

    使用的是centos7. 1.下载最新redis源码,解压(2016-05-12最新版本为3.2.0,3.0及以上才有官方集群) 2.进入源码根目录(此目录下的redis-stable目录),找到ut ...

  8. 移动web——bootstrap媒体对象

    基本模板 1.这些组件都具有在文本内容的左或右侧对齐的图片(就像博客评论或 Twitter 消息等) <div class="media"> <div class ...

  9. JS——三元表达式

    三元表达式: var n1 = 2 > 3 ? true : false; alert(n1);//返回false

  10. [Windows Server 2008] IIS自带FTP配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS自带FT ...