【问题标题】:Select a count of contacts from accounts using FetchXML使用 FetchXML 从帐户中选择联系人计数
【发布时间】:2018-09-10 17:48:05
【问题描述】:

有谁知道 FetchXML 格式以便为帐户选择联系人计数?

例如,我将有一个 AccountID 列表,我将对其进行 IN 过滤,我只需要 AccountID 和该帐户的联系人的整数计数。

已解决(见下文),只需对按帐户分组稍作更改。这是我使用的最终 fetchxml:

<fetch xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" aggregate="true" distinct="false" mapping="logical"> 
<entity name="contact">
<attribute name="contactid" alias="recordcount" aggregate="count" />
<link-entity name="account" to="accountid" alias="accountid">
<attribute name="accountid" alias="accountid" groupby="true" />
<filter>
<condition attribute="accountid" operator="in">
<value>708039fd-f7b1-e811-a973-000d3af4a510</value>
<value>0a8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>428139fd-f7b1-e811-a973-000d3af4a510</value>
<value>4a8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>618139fd-f7b1-e811-a973-000d3af4a510</value>
<value>9f8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>ae8239fd-f7b1-e811-a973-000d3af4a510</value>
</condition>
</filter>
</link-entity>
</entity>
</fetch>

【问题讨论】:

    标签: dynamics-crm fetchxml dynamics-crm-webapi


    【解决方案1】:

    您可以在 parentcustomerid 上使用 group by 进行聚合提取。

    <fetch distinct='false' mapping='logical' aggregate='true'> 
        <entity name='contact'> 
            <attribute name='contactid' alias='contact_count' aggregate='countcolumn' /> 
            <attribute name='parentcustomerid' alias='parentcustomerid' groupby='true' />
            <link-entity name='account' from='accountid' to='parentcustomerid'>
                <filter type='and'>
                    <condition attribute='accountid' operator='in'>
                        <value>{00000000-0000-0000-0000-000000000001}</value>
                        <value>{00000000-0000-0000-0000-000000000002}</value>
                    </condition >
                </filter>
            </link-entity> 
        </entity> 
    </fetch>
    

    【讨论】:

    • 这将为您提供所有列出的帐户的联系人总数,而不是每个帐户
    • @jasonscript 帐户链接实体上的过滤器会将联系人限制为列出的帐户。 groupby = true 的 parentcustomerid 将导致每个列出的帐户有一行,其中包含特定于该帐户的联系人计数。
    • 使用链接实体有效,我只需将 groupby 移动到帐户对象,这样它就可以为我提供具有正确计数的唯一 AccountId。
    【解决方案2】:

    use Rollup field 这很简单。

    然后就可以轻松查询计数了。

    <fetch>
      <entity name="account" >
        <attribute name="name"  />
        <attribute name="new_contactcountrollup"  />
      </entity>
    </fetch>
    

    更新:

    为什么我说 fetchxml 不支持子查询,因为我正在考虑第二种解决方案。

    --using just JOIN
    SELECT a.accountid, count(1) AS [contact count] FROM contact c
    INNER JOIN account a
    ON c.parentcustomerid = a.accountid
    WHERE a.accountid IN  (
    '{0ACDC4F5-4885-E811-A967-000D3A1A9407}', '{BA41CEBA-199F-E811-A96B-000D3A1A9EFB}')
    GROUP BY a.accountid
    
    --using SUBQUERY
    SELECT a.accountid,
        (SELECT Count(1) FROM contact c WHERE c.parentcustomerid = a.accountid) AS [contact count]
        FROM account a
        WHERE a.accountid IN (
    '{0ACDC4F5-4885-E811-A967-000D3A1A9407}', '{BA41CEBA-199F-E811-A96B-000D3A1A9EFB}')
    

    【讨论】:

    • 它不一定是子查询,上面的答案与聚合、分组和计数工作正常:)
    • @JonasRapp 我知道,但意见不同。 CRM OOB 汇总字段是我的答案。
    • 绝对 :) 评论是对子查询语句,我现在看到你编辑了 :)
    • @JonasRapp 我正在考虑第二种解决方案 - 请参阅我的更新 :)
    • 是的,我想是的。但即使在 SQL 中,这也是一种简单地按帐户计算联系人的混乱方式;) Group By 也会更好。在我看来.... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2017-05-10
    • 2021-02-03
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多