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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

SQL operator support (CROSS APPLY)

(1) ShareShare
ReportReport
Posted on by 514
Hello:
I haven't found anything searching on my own. I'm wondering if X++ Query, SQL, or QueryBuilder support the CROSS APPLY SQL operator in some manner. The issue I have is I need a value from SALESTABLE but going through CUSTINVOICETRANS give me too many rows. I got the below SQL working within SSMS, but wondering how to translate the equivalent in an AOT Query, X++ SQL, or QueryBuilder Initially, it seems to make sense to use QueryBuilder to achieve what I need, but I'm open to other options if I can do it all without too much fuss (if that makes sense). Thank you!
 
SELECT
T1.ACCOUNTNUM,
T12.STREET,
T12.CITY,
T12.STATE AS 'STATE',
CASE WHEN DETAILS.[CUSTOM FIELD] <> '' THEN DETAILS.[CUSTOM FIELD]
            ELSE T2.INVOICE
END AS 'INVOICE NUMBER',
FROM CUSTTRANSOPEN T1 
    JOIN CUSTTRANS T2
    ON T2.ACCOUNTNUM = T1.ACCOUNTNUM
    AND T1.REFRECID = T2.RECID
        JOIN CUSTTABLE T3
        ON T3.ACCOUNTNUM = T2.ACCOUNTNUM
            JOIN DIRPARTYTABLE T10
            ON T10.RECID = T3.PARTY
                JOIN LOGISTICSLOCATION T11
                ON T11.RECID = T10.PRIMARYADDRESSLOCATION
                    JOIN LOGISTICSPOSTALADDRESS T12
                    ON T12.LOCATION = T11.RECID
        
        CROSS APPLY
        (
            SELECT TOP 1 T4.SALESID, T5.LINENUM FROM 
            CUSTINVOICEJOUR T4
                JOIN CUSTINVOICETRANS T5
                ON T5.SALESID = T4.SALESID
                AND T5.INVOICEID = T4.INVOICEID
                AND T5.INVOICEDATE = T4.INVOICEDATE
                AND T5.NUMBERSEQUENCEGROUP = T4.NUMBERSEQUENCEGROUP
                    JOIN SALESTABLE T13
                    ON T13.SALESID = T5.SALESID
            WHERE
                ...
        ) DETAILS
WHERE 
...
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    235,936 Most Valuable Professional on at
    SQL operator support (CROSS APPLY)
    There is support of CROSS APPLY. 
     
    The description of your problem is vague, but it suggests that you can simply use joins (instead of CROSS APPLY) and you tried to do it, but you had a bug there. The correct approach will be fixing the bug, in my opinion.
     
    Please tell us what data you want to get, show us your code (remove everything unrelated to the problem in question) and tell us how the observed behaviour differs from the expected one.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 756 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 629 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 514 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans