Skip to main content

Notifications

Announcements

No record found.

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

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

    It worked. Great, thank you.

  • 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>


  • 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>

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans