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

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