web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

FetchXML with aggregate 'min' retrieves multiple results

(0) ShareShare
ReportReport
Posted on by

Hi, 

I'm trying to retrieve the activity with the minimum scheduled start date as follows:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
                          <entity name='activitypointer'>
                            <attribute name='activitytypecode' groupby='true' alias='activitytypecode_min'/>
                            <attribute name='scheduledstart' aggregate='min' alias='scheduledstart_min' />
                               <filter type='and'>
                              <condition attribute='statecode' operator='in'>
                                <value>0</value>
                                <value>3</value>
                              </condition>
                              
                            </filter>
                            <link-entity name='opportunity' from='opportunityid' to='regardingobjectid' alias='ae'>
                              <filter type='and'>
                                <condition attribute='opportunityid' operator='eq' uitype='opportunity'  value="{E8409D2A-2582-E511-80FD-3863BB3630D8}" />
                              </filter>
                            </link-entity>
                          </entity>
                        </fetch>


But instead of retrieving a single result, the fetchxml tester returned multiple results as follows:

<resultset morerecords="0">
  <result>
    <scheduledstart_min date="11/7/2015" time="8:00 AM">2015-11-07T08:00:00+02:00</scheduledstart_min>
    <activitytypecode_min name="Email" formattedvalue="4202">4202</activitytypecode_min>
    <activitytypecode_min_activitytypecodename>Email</activitytypecode_min_activitytypecodename>
  </result>
  <result>
    <scheduledstart_min date="11/8/2015" time="8:00 AM">2015-11-08T08:00:00+02:00</scheduledstart_min>
    <activitytypecode_min name="Phone Call" formattedvalue="4210">4210</activitytypecode_min>
    <activitytypecode_min_activitytypecodename>Phone Call</activitytypecode_min_activitytypecodename>
  </result>
  <result>
    <scheduledstart_min date="11/4/2015" time="8:00 AM">2015-11-04T08:00:00+02:00</scheduledstart_min>
    <activitytypecode_min name="Task" formattedvalue="4212">4212</activitytypecode_min>
    <activitytypecode_min_activitytypecodename>Task</activitytypecode_min_activitytypecodename>
  </result>
</resultset>


Can you help me with why it is retrieving multiple results although it has an aggregate function? 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    I apologize I just notices that my group by field has different values in the results.

    What I need to do is that I want to get the activity with minimum scheduled start date and then get the activity code of that activity.

    Is there a way to do that with aggregate function ?

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi SB,

    If I understand, you want the most recent activity of any type, and then get the type for it. That's easy using the query you have, just remove the groupby condition in the activitytypecode. This will tell it not to group by anything, returning the single record, but it'll still have the activitytypecode column in the results.

    Hope this helps! I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

     Aiden

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    SB - on second thought, there's no need for an aggregate. Just add an order condition on the date so you get the oldest (or newest) date - details on the Order node: nishantrana.me/.../sorting-on-link-entitys-attribute-in-fetch-xml-crm-2011

    Then since you're getting your desired record first, limit the query results to just one record by updating the fetch node to say:

    <fetch count="1">

    This will give you one row with the values you want.

    Hope this helps! If so I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

     Aiden

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans