1

我正在寻求将下面的 SQL 查询转换为 FetchXML 查询的帮助。

背景/目的:作为构建 PowerApps 流程的一部分,我需要列出 Microsoft Dataverse(尤其是 Dynamics 365)中的行,以便在业务机会所有者需要的操作超过 2 次时,我可以收到每日电子邮件通知以发送给他们逾期天数。日期的触发器位于商机记录的子记录 (1:N) 上。因为可能并且将会有多个子记录使用此日期字段 - 我想提取最大值日期,但需要查询结果显示机会值,然后在该机会记录行中显示这个单一子记录最大值。

我可以获取下面的 SQL 查询来提取我正在寻找的结果,但它需要采用 FetchXML 查询格式。

SELECT opp.opportunityid
        ,opp.name
        ,opp.customeventtypename
        ,opp.ownerid
        ,opp.owneridname
        ,opp.statecodename
        ,opp.totalattendance
        ,A.MaxArrivalDateTime
FROM opportunity opp
INNER JOIN
(
SELECT c.opportunity
        ,max(c.arrivaldatetime) MaxArrivalDateTime     
     FROM oppchildrecords c
     GROUP BY c.opportunity
)A ON (A.opportunity = opp.opportunityid)
WHERE opp.customeventtypename = 'EventX'
AND opp.statecodename = 'Open'
AND opp.totalattendance <> '0'
AND opp.totalattendance is not null
AND A.MaxArrivalDateTime <= DATEADD(day, -2, GETDATE())

我使用的工具允许您显示 FetchXML 查询,但它会将其分解为三个单独的查询,因此我试图将它们全部合二为一。以下是它生成的三个 FetchXML 查询:

1)

<fetch xmlns:generator='MarkMpn.SQL4CDS'>
  <entity name='opportunity'>
    <attribute name='opportunityid' />
    <attribute name='name' />
    <attribute name='customeventtype' />
    <attribute name='ownerid' />
    <attribute name='statecode' />
    <attribute name='totalattendance' />
    <link-entity name='customeventtype' to='customeventtype' from='customeventtypeid' alias='opportunity_customeventtype' link-type='outer' />
    <filter>
      <filter>
        <filter>
          <condition attribute='customeventtype' entityname='opportunity_customeventtype' operator='eq' value='EventX' />
          <condition attribute='statecode' operator='eq' value='0' />
        </filter>
        <condition attribute='totalattendance' operator='ne' value='0' />
      </filter>
      <condition attribute='totalattendance' operator='not-null' />
    </filter>
    <order attribute='opportunityid' />
  </entity>
</fetch>
<fetch xmlns:generator='MarkMpn.SQL4CDS' aggregate='true'>
  <entity name='oppchildrecords'>
    <attribute name='opportunity' alias='opportunity' groupby='true' />
    <attribute name='arrivaldatetime' alias='MaxArrivalDateTime' aggregate='max' />
    <order alias='opportunity' />
  </entity>
</fetch>
<fetch xmlns:generator='MarkMpn.SQL4CDS'>
  <entity name='oppchildrecords'>
    <attribute name='opportunity' />
    <attribute name='arrivaldatetime' />
  </entity>
</fetch>
4

0 回答 0