crm使用FetchXml聚合查询
/* 创建者:菜刀居士的博客
* 创建日期:2014年07月08号
*/
namespace Net.CRM.FetchXml
{
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// 使用FetchXml聚合查询
/// </summary>
public class FetchXmlDemo
{
/* 特别提示:FetchXML 包含使您可以计算总和、平均值、最小值、最大值和计数的分组和聚合函数。
* 在查询中仅仅能指定一个 aggregate 属性,并且不能使用 distinct keyword。要创建的聚合的属性。
* 请设置keywordaggregate到true,然后指定有效的实体名称。 属性名称,和别名 (变量名)。
* 同一时候必须指定要运行的聚合的类型。
*/
/// <summary>
/// 总和
/// sql: select sum(new_value) as 'new_value_sum' from account
/// </summary>
public void Sum(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_value' alias='new_value_sum' aggregate='sum' />
</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["new_value_sum"]).Value).Value;
}
}
/// <summary>
/// 平均值
/// sql: select avg(new_value) as 'new_value_avg' from account
/// 当crm计算数据的平均值时,不考虑 Null 值。
可是。会使用零 (0)。
/// </summary>
public void Avg(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_value' alias='new_value_avg' aggregate='avg' />
</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["new_value_avg"]).Value).Value;
}
}
/// <summary>
/// 计算有多少个记录
/// sql: select count(*) from account
/// </summary>
public void Count(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_name' alias='new_name_count' aggregate='count' />
</entity>
</fetch>";
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (ec != null && ec.Entities.Count > 0)
{
Entity en = ec.Entities[0];
//获取结果
int value = (Int32)((AliasedValue)en["new_name_count"]).Value;
}
}
/// <summary>
/// 计算有多少个记录(针对指定的列名)
/// sql: select count(distinct new_name) from account
/// </summary>
public void CountColumn(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_name' alias='new_name_count' aggregate='countcolumn' distinct='true' />
</entity>
</fetch>";
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (ec != null && ec.Entities.Count > 0)
{
Entity en = ec.Entities[0];
//获取结果
int value = (Int32)((AliasedValue)en["new_name_count"]).Value;
}
}
/// <summary>
/// 最大值
/// sql: select max(new_value) as 'new_value_max' from account
/// 当crm计算数据的最大值时,不考虑 Null 值。可是,会使用零 (0)。
/// </summary>
public void Max(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_value' alias='new_value_max' aggregate='max' />
</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["new_value_max"]).Value).Value;
}
}
/// <summary>
/// 最小值
/// sql: select min(new_value) as 'new_value_min' from account
/// 当crm计算数据的最小值时,不考虑 Null 值。可是。会使用零 (0)。
/// </summary>
public void Min(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_value' alias='new_value_min' aggregate='min' />
</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["new_value_min"]).Value).Value;
}
}
/// <summary>
/// 多个聚合
/// sql: select count(*) as 'new_value_count',max(new_value) as 'new_value_max',
/// min(new_value) as 'new_value_min' from account
/// </summary>
public void CountAndMaxAndMin(IOrganizationService service)
{
string fetchXml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='account'>
<attribute name='new_value' alias='new_value_count' aggregate='count' />
<attribute name='new_value' alias='new_value_max' aggregate='max' />
<attribute name='new_value' alias='new_value_min' aggregate='min' />
</entity>
</fetch>";
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (ec != null && ec.Entities.Count > 0)
{
Entity en = ec.Entities[0];
//获取结果
int count_value = (Int32)((AliasedValue)en["new_value_count"]).Value;
decimal max_value = ((Money)((AliasedValue)en["new_value_max"]).Value).Value;
decimal min_value = ((Money)((AliasedValue)en["new_value_min"]).Value).Value;
}
}
}
}
crm使用FetchXml聚合查询的更多相关文章
- crm使用FetchXml分组聚合查询
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月09号 */ namespace Net.CRM.FetchXml { using System; using Micr ...
- Dynamics CRM 2015/2016 Web API:聚合查询
各位小伙伴们,今天是博主2016年发的第一篇文章.首先祝大家新年快乐.工资Double,哈哈.今天我们来看一个比較重要的Feature--使用Web API运行FetchXML查询! 对的,各位.你们 ...
- python操作mongodb之二聚合查询
#聚合查询 from pymongo import MongoClient db = MongoClient('mongodb://10.0.0.9:27017/').aggregation_exam ...
- [SQL基础教程] 3-1 对表进行聚合查询
[SQL基础教程] 3-1 对表进行聚合查询 聚合函数 用于合计的函数称为聚合函数或者集合函数 COUNT SUM AVG MAX MIN SELECT COUNT(*) FROM table; SE ...
- 开发中使用mongoTemplate进行Aggregation聚合查询
笔记:使用mongo聚合查询(一开始根本没接触过mongo,一点一点慢慢的查资料完成了工作需求) 需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话.地址.支付总金额以及总 ...
- mongodb高级聚合查询
在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysql复杂很多. 注:本文基于 mo ...
- ThinkPHP 数据库操作(四) : 聚合查询、时间查询、高级查询
聚合查询 在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数.所有用户的最大积分.用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括: 用法示例: ...
- ElasticSearch 6.2 Mapping参数说明及text类型字段聚合查询配置
背景: 由于本人使用的是6.0以上的版本es,在使用发现很多中文博客对于mapping参数的说明已过时.ES6.0以后有很多参数变化. 现我根据官网总结mapping最新的参数,希望能对大家有用处. ...
- orm分组,聚合查询,执行原生sql语句
from django.db.models import Avg from app01 import models annotate:(聚合查询) ret=models.Article.objects ...
随机推荐
- C# 中datagridview行里面有三个cheeckbox,要控制成三选一。
我之前有试过在cellendedit中处理,可以达成效果,当不符合用户打单的界面要求.该事件是在单元格编辑结束之后, 当用户选中两个checkbox,且焦点不移开时,界面上会出现有两个checkbox ...
- VS 2013上Python的配置
最近有点不务正业,去看了下Python (主要是学校OJ有这个语言,然后可以轻松解决大数据问题,不要说我太坑~~~) 目前感觉python和matlab有些类似,缺少了变量类型声明,总感觉自己写出来的 ...
- C语言深度剖析---const关键字(转载)
const是constant的缩写,是恒定不变的意思.被const修饰的值,是只读变量. 1.const修饰只读变量,具有不变性 #include <stdio.h> int m ...
- Gauss elimination Template
Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...
- dotnet tools 运行 dotnet run
dotnet tools 运行 dotnet run dotnet run 命令介绍 前言 本篇主要介绍 asp.net core 中,使用 dotnet tools 运行 dotnet run 之后 ...
- MediaPlayer视频播放器
android视频播放 根据apidemo重写.代码如下: package com.jamdeo.tv.livetv.player; import android.media.AudioManager ...
- Java For循环效率试验
非常奇怪的结果! ..! 测试方法如以下 public class Main { public static void main(String[] args){ long ti = System.cu ...
- Robot Framework与Web界面自动化测试学习笔记:如何判断单选框的选中状态
单选按钮是个常见的html元素,在网页中往往提供一组单选按钮来做选项. 这样在自动化测试用例中需要判断当前选中的按钮是否与预期的一直. 可以这样来操作: ${value} Get Element ...
- 基于visual Studio2013解决C语言竞赛题之0710排序函数
题目
- Study notes for Sparse Coding
Sparse Coding Sparse coding is a class of unsupervised methods for learning sets of over-complete ba ...