How to convert this SQL Query to FetchXML Query for Power Automate Flow?

287 Views Asked by At

I'm looking for help in converting the SQL query below into a FetchXML Query.

Background/Purpose: As part of building a PowerApps' Flow, I need to List Rows in Microsoft Dataverse (Dynamics 365 in particular) so that I can get a daily email notification to go out to Opportunity Owners when their required action is more than 2 days past due. The trigger for the date is on a child record (1:N) of the Opportunity record. Because there could and will be multiple child records using this date field - I'm wanting to pull the max value date, but need the query results to show Opportunity values and then this singular child record max value within that Opportunity record row.

I can get the SQL query below to pull the results I am looking for, but it needs to be in FetchXML query format.

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

The tool I'm using lets you display the FetchXML Query, but it breaks it down into three separate queries, so I'm trying to get them all into one. Here are those three FetchXML queries it produces:

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

There are 0 best solutions below