dynamics crm 2011 - Aggregate function with FetchXML



我需要获取在某个字段中具有最大日期的实体。我用 Stunnware 尝试了下面的代码,但它给了我一个错误,即 MAX 函数无效。

  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
    <entity name='field1'>
      <attribute name='field2' />
      <attribute name='field3' />
      <attribute name='field4' />
      <order attribute='field1' descending='false' />
      <link-entity name='contact' from='field1' to='otherfield' alias='ac'>
          <filter type='and'>
          <condition attribute='field5' operator='eq' value='123456' />
           </filter>
      </link-entity>
     <link-entity name='secondentity' from='field2' to='otherfield' visible='false' link-type='outer' alias='a_6c61a84be522e31194080050569c4325'>
         <attribute name='date' alias='maxdate' aggregate='max' />
      </link-entity>
   </entity>
   </fetch>

你能帮我指出我正在犯的错误吗?

事实证明它不起作用:

我的查询中存在几个问题:

1-根据Paul Way的回复,我的fetch xml丢失了aggregate="true"

2-聚合函数不适用于Order属性

3-如果要在使用聚合函数时检索属性,则必须groupby它们并添加alias

4-聚合函数MAX不能应用于日期类型。

所以我的另一个解决方案是按降序检索所有日期,然后我将使用检索到的第一个实体。

这里有一个很好的例子:http://msdn.microsoft.com/en-us/library/gg309565.aspx

<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='opportunity'> 
       <attribute name='estimatedvalue' alias='estimatedvalue_max' aggregate='max' /> 
    </entity> 
</fetch>

对于上面的 XML,应该是这样的:

<fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true">
    <entity name="myentity">
        <attribute name="personname" />
        <order attribute="personname" descending="false" />
        <link-entity name="mysecondentity" from="personid" to="secondpersonid" visible="false" link-type="outer" alias="aa">
            <attribute name="date" alias='date_max' aggregate="max" />
        </link-entity>
    </entity>
</fetch>

我刚刚有同样的经历。长话短说:在 CRM 2011 中,日期字段不可能出现总和、平均值、最小值和最大值。在 CRM 2013 中,MIN 和 MAX 适用于日期字段。

因此,在 2011 年,唯一的方法是选择所有内容并自己完成工作,或者选择数据,按它排序并将 pagesize 设置为 1,以获得最大值或最小值。

最新更新