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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Fetchxml group by count issues

(0) ShareShare
ReportReport
Posted on by 200

I am trying to get the count of distinct organisations/accounts with the below fetchxml.

The below query has two employments with the same organisation so I am expecting count to be 1. However it is returning 2 . Any solutions? Thanks.

<fetch aggregate="true" returntotalrecordcount="true">
<entity name="sb_sb_employment_sb_application"> // Many to many relationship table
<filter type="and">
<condition attribute="sb_applicationid" operator="eq" value="GUID" />
</filter>
<link-entity name="sb_employment" from="sb_employmentid" to="sb_employmentid" link-type="inner" alias="employment">

<filter>
<condition attribute="sb_dateto" operator="null" />
<condition attribute="sb_organisationid" operator="not-null" />
</filter>


<link-entity name="account" from="accountid" to="sb_organisationid" link-type="inner">
<attribute name="accountid" alias="AccountKey" groupby="true" />
<attribute name="accountname" alias="billingname" aggregate="count" />
</link-entity>
</link-entity>
</entity>
</fetch>

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at
    RE: Fetchxml group by count issues

    Use account id in place of account name in aggregate function.

    You can try below XML:

    <fetch aggregate="true" returntotalrecordcount="true">

    <entity name="sb_sb_employment_sb_application"> // Many to many relationship table

    <filter type="and">

    <condition attribute="sb_applicationid" operator="eq" value="GUID" />

    </filter>

    <link-entity name="sb_employment" from="sb_employmentid" to="sb_employmentid" link-type="inner" alias="employment">

    <filter>

    <condition attribute="sb_dateto" operator="null" />

    <condition attribute="sb_organisationid" operator="not-null" />

    </filter>

    <link-entity name="account" from="accountid" to="sb_organisationid" link-type="inner" alias="acc">

    <attribute name="accountid" alias="AccountKey" groupby="true" />

    <attribute name="accountid" alias="billingid" aggregate="count" />

    </link-entity>

    </link-entity>

    </entity>

    </fetch>

  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: Fetchxml group by count issues

    To get a distinct count, you need to include a distinct attribute and also use countcolumn rather than count:

    <fetch aggregate="true" returntotalrecordcount="true">
    <entity name="sb_sb_employment_sb_application"> // Many to many relationship table
    <filter type="and">
    <condition attribute="sb_applicationid" operator="eq" value="GUID" />
    </filter>
    <link-entity name="sb_employment" from="sb_employmentid" to="sb_employmentid" link-type="inner" alias="employment">
    <filter>
    <condition attribute="sb_dateto" operator="null" />
    <condition attribute="sb_organisationid" operator="not-null" />
    </filter>
    
    <link-entity name="account" from="accountid" to="sb_organisationid" link-type="inner">
    <attribute name="accountid" alias="AccountKey" groupby="true" />
    <attribute name="accountname" alias="billingname" aggregate="countcolumn" distinct="true" />
    </link-entity>
    </link-entity>
    </entity>
    </fetch>


  • SwathiD Profile Picture
    200 on at
    RE: Fetchxml group by count issues

    It worked. Great, thank you.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans