crm使用FetchXml分组聚合查询
/* 创建者:菜刀居士的博客
* 创建日期: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分组聚合查询的更多相关文章
- orm分组,聚合查询,执行原生sql语句
from django.db.models import Avg from app01 import models annotate:(聚合查询) ret=models.Article.objects ...
- Solr分组聚合查询之Facet
摘要: Solr的分组聚合是一个笼统的概念,目的就是把查询结果做分类,有多种方式可以做到很类似的结果.也正是由于它们的不同表现,可以适合于多种场景. 何为Facet Facet是一种手段,用来将搜索结 ...
- Solr分组聚合查询之Group
摘要: Solr对结果的分组处理除了facet还可以使用group.Solr的group是根据某一字段对结果分组,将每一组内满足查询的结果按顺序返回. Group对比Facet Group和Facet ...
- SQL分组聚合查询练习(SQL Server和Oracle相似)20190514
先建表 CREATE TABLE [dbo].[orderdt_jimmy]( ,) NOT NULL, [order_nid] [int] NOT NULL, ) NOT NULL, [qty] [ ...
- Django对postgresql数据库进行分组聚合查询
action(methods=['GET'], detail=False, url_path='count') def count(self, request): """ ...
- solrcloud jsonfacet分组聚合 unique计数不准确
jsonfacet分组聚合查询 unique.hll函数问题: 对不同的值进行估算,并非准确的值, 优点:节省内存消耗,用分组算法对不同的值count进行估算 缺点:无法准确统计count(disti ...
- django第10天(聚合查询,常用字段)
django第10天 聚合查询 聚合函数的使用场景 单独使用:不分组,只查聚合结果 分组使用:按字段分组,可查分组字段与聚合结果 导入聚合函数 from django.db.models import ...
- 浅析MySQL使用 GROUP BY 分组聚合与细分聚合
原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7521278.html: 若有错误,请评论指出,谢谢! 1. 聚合函数(Aggregate Function ...
- Flask聚合函数(基本聚合函数、分组聚合函数、去重聚合函数))
Flask聚合函数 1.基本聚合函数(sun/count/max/min/avg) 使用聚合函数先导入:from sqlalchemy import func 使用方法: sun():func.sum ...
随机推荐
- java学习笔记_网络
客户端 import java.io.*; import java.net.*; public class DailyAdviceClient { public void go() { try { S ...
- [ SCOI 2008 ] 着色方案
\(\\\) \(Description\) 给出\(K\)种颜料各自的个数\(C_i\),每一个颜料只够涂一个格子,求将颜料用完,涂一排格子,每个格子只能涂一次的条件下,相邻两个格子的颜色互不相同的 ...
- jQuery五屏轮播手风琴切换代码
jQuery五屏轮播手风琴切换代码 在线演示本地下载
- sql server 大数据跨服务器迁移表数据——使用链接服务器
1.创建链接服务器(填写链接服务器.远程登录.使用密码) 2.188.188.1.177是远程的 select count(*) from [188.188.1.177].BigDataAnalysi ...
- [Windows Server 2012] Tomcat安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Win20 ...
- C#使用Win32函数的一些类型转换
C#在访问Win 32 Api时需要处理C 结构与C#结构的映射,这在MSDN以及许多Blog上都可以找到参考的资料.Win 32 中有一些定义复杂的Struct,这些结构体拥有长度固定的数组或者一些 ...
- JS页面跳转和js对iframe进行页面跳转、刷新
一.js方式的页面跳转1.window.location.href方式 <script language="JavaScript" type="text/ja ...
- node版本管理工具nvm安装使用教程
一些安装包依赖一定的node版本,可以采用nvm管理node, 可以快速的进行版本切换. 操作系统: windows10, x64 常见版本工具: 1. nvmw, nvmm install node ...
- 笔记,js对象浅析
学习笔记, 来源:http://www.cnblogs.com/zuiyirenjian/p/3535126.html 作者:醉意人间 此外,关于自运行函数可参考 http://benalman. ...
- JSONP代码收藏
摘抄自jQuery,用于JSONP请求. var callback = 'callback_' + (new Date() - 0), url = 'http://localhost/', scrip ...