crm使用FetchXml分组聚合查询
2014-07-09 09:09
513 查看
/* 创建者:菜刀居士的博客
* 创建日期: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;
}
}
}
}
* 创建日期: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分组聚合查询
- SSRS 系列 - 使用带参数的 MDX 查询实现一个分组聚合功能的报表
- Oracle中分组查询时keep的使用[转][最简][易懂]
- SQL字符串分组聚合(分组后的数据查询后用逗号隔开)
- 条件查询---6.5.2: 投影、聚合和分组
- crm2011 将FetchXml查询转化为Query
- CRM2011 将Query查询转化为FetchXml
- Access使用查询--1.2.用选择查询进行分组数据的计算
- DataTable、List使用groupby进行分组和分组统计;List、DataTable查询筛选方法
- Sql Server -partition by 使用说明 -分组排序-分组并查询其他字段
- crm2011 使用SOAP 查询单个记录 Retrieve
- 【原创】StreamInsight查询系列(六)——基本查询操作之分组聚合
- SQL查询入门---聚合函数的使用和数据的分组
- Hibernate的查询 标准(Criteria)查询 聚合和分组
- Oracle基本分组查询group by的使用
- 使用SQL语句查询每个分组的前N条记录
- SQL分组查询及聚集函数的使用
- Hibernate的查询 标准(Criteria--Projections)查询----- 聚合和分组
- 在 SELECT 查询中使用分组集
- crm使用rest端点的odata系统查询选项