web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Convert SQL query to X++ (either Query builder or pure X++)

(0) ShareShare
ReportReport
Posted on by

I'm writing a query to select all vendors, their last PO date, last Payment date and last Invoice date. Initially, I wrote a while select on all vendors (crosscompany, grouped by accountnum), then wrote select statements within the while statement to select the firstonly transaction from the 3 tables (PurchTable, VendTrans and VendInvoiceJour) order by [date field] desc. 
This process takes 10 min with tiny amounts of data, and up to 3 hours in QA, so I built the query below in SQL and it runs fine. My question is, is it even possible to convert this kind of query to X++/QueryBuilder (I want to IGNORE all dataareaids):

SELECT DISTINCT t1.AccountNum, t1.InvoiceAccount, t1.VendGroup, t1.Party, t1.NBDCMMSAllowInvoices, t2.InvoiceDate, t3.CreatedDateTime, t4.TransDate FROM VendTable AS t1
LEFT JOIN
(SELECT OrderAccount AS AccountNum, max(InvoiceDate) AS InvoiceDate FROM VendInvoiceJour GROUP BY OrderAccount) t2
ON t1.AccountNum = t2.AccountNum
LEFT JOIN
(SELECT OrderAccount AS AccountNum, max(CreatedDateTime) AS CreatedDateTime FROM PurchTable GROUP BY OrderAccount) t3
ON t1.AccountNum = t3.AccountNum
LEFT JOIN
(SELECT AccountNum, max(TransDate) AS TransDate FROM VendTrans GROUP BY AccountNum) t4
ON t1.AccountNum = t4.AccountNum


The tricky part is:
1. I only want 1 instance of each AccountNum (we have 26 companies, and vendors are replicated across all companies, so a single real-world vendor has 26 entries in the table, each with a different dataareaid).

2. I want the last PO/Invoice/Payment across ALL companies (so if Vendor A has 1 PO with date 01/01/2018 in Company A, but no POs in all other companies, I would want a single result for Vendor A with Last Po = 01/01/2018)

*This post is locked for comments

I have the same question (0)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans