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

Announcements

No record found.

News and Announcements icon
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

    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

    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

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans