简体   繁体   中英

Dynamics CRM FetchXML equivalent of SQL with sub query aggregate column values

I have a requirement to generate a CRM Report (online, so have to use FetchXML) that shows Customers plus the number of products they ordered, with a column for each (pre-defined) category.

The entities are effectively: Customers, Orders, Orderlines and Products. I have created test data in SQL Server (2008 R2) and the following SQL statement generates the correct results, but I'd like to know if this is possible in FetchXML, and if so, how.

I did try using sql2fetchxml.com, but it showed "Error: Unsupported field expression"

select c.Description, 

(select sum(l1.Quantity) from Customers c1 
Join Orders o1 on o1.CustomerID = c1.ID
Join OrderLines l1 on l1.OrderID = o1.ID
Join Products p1 on l1.ProductID = p1.ID and p1.Category = 'A'
where c1.ID = c.ID) CategoryA_Quantity,

(select sum(l2.Quantity) from Customers c2 
Join Orders o2 on o2.CustomerID = c2.ID
Join OrderLines l2 on l2.OrderID = o2.ID
Join Products p2 on l2.ProductID = p2.ID and p2.Category = 'B'
where c2.ID = c.ID) CategoryB_Quantity,

(select sum(l3.Quantity) from Customers c3
Join Orders o3 on o3.CustomerID = c3.ID
Join OrderLines l3 on l3.OrderID = o3.ID
Join Products p3 on l3.ProductID = p3.ID and p3.Category = 'C'
where c3.ID = c.ID) CategoryC_Quantity

from Customers c

You can't perform sub queries in CRM. You could perform multiple queries, but not creating sub queries as you have ti defined.

While not Dynamics-based, you could create a flow in Power Automate to duplicate your SQL script that would then output the report. I use this method to populate a portal page with a query that is similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM