Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

FetchXML query with multiple Counts

Posted on by

I am trying to create  FetchXML query that will give me the Total Number of Open Leads and also the Total Number of Open Leads that do not have Open Activities against them.

I have the two queries:

Open Leads no Act:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true" >
  <entity name="lead" >
    <link-entity name="task" from="regardingobjectid" to="leadid" link-type="outer" alias="task" >
      <attribute name="regardingobjectid" aggregate="count" alias="TotalOpenLeadsNoActivities" />
    </link-entity>
    <filter type="and" >
      <condition entityname="task" attribute="regardingobjectid" operator="null" />
      <condition entityname="lead" attribute="statecode" operator="eq" value="0" />
    </filter>
  </entity>
</fetch>

AllOpenLeads:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true" >
  <entity name="lead" >
    <attribute name="leadid" aggregate="count" alias="TotalOpenLeads" />
    <filter type="and" >
      <condition entityname="lead" attribute="statecode" operator="eq" value="0" />
    </filter>
  </entity>
</fetch>

Is there a way to merge them so that I simply get two "COUNT" numbers returned by one query?

Thanks.

*This post is locked for comments

  • Suggested answer
    Zohaib Uddin Khan Profile Picture
    Zohaib Uddin Khan 2,811 on at
    RE: FetchXML query with multiple Counts

    Single FetchXML can't return two separate counts. Thanks.

  • Suggested answer
    prt33k Profile Picture
    prt33k 6,907 on at
    RE: FetchXML query with multiple Counts

    Hi,

    Using fetchxml, this wont be possible. You need to use SQL for this.

    However there is a option to use Export Service and then you can make such queries.

    You can read about Export Service here : msdn.microsoft.com/.../mt788315.aspx

    Thanks,

    PS

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans