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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

A bit help on X++ select query

(0) ShareShare
ReportReport
Posted on by

Hi guys,

Need some help on sql query in x , which I'm not sure why it is not working, but if I test it in SSMS it is fine.

First of all I have a custom table (lets say TableInvoice) which is to hold customer invoice. This will linked to table CustTrans.

When I'm using this query : 

while select TableInvoice
    where TableInvoice.CustAccount == "CustomerA"
join custTrans where custTrans.AccountNum == TableInvoice.CustAccount
                && custTrans.Invoice == TableInvoice.InvoiceId

The 2 table give me the right value.

But because in my TableInvoice could have multi row of same invoice with same customer while I only need one record, so I change it to this : 

while select InvoiceId, CustAccount from TableInvoice group by InvoiceId, CustAccount
    where TableInvoice.CustAccount == "CustomerA"
join AccountNum, Invoice, RecId from custTrans where custTrans.AccountNum == TableInvoice.CustAccount
            && custTrans.Invoice == TableInvoice.InvoiceId

So I adding grouping for the 1st table, my custom table "TableInvoice". In debug, I can see the record TableInvoice is correct, but CustTrans empty.

If I put this grouping query and join in SSMS, it is returning the correct record that I want, like this : 

select InvoiceId, CustAccount from TABLEINVOICE a
join custTrans b on b.AccountNum = a.CustAccount and b.INVOICE = a.InvoiceId
where a.CustAccount = 'CustomerA'  
	group by a.InvoiceId, a.CustAccount

How to get the record CustTrans with the 2nd query (X query), because I need to add another join to CustTransOpen, but currently cannot since CustTrans empty.

Thanks,

I have the same question (0)
  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Hi, When you group by on fields in select statement, you can retrieve those fields only. If you tell us your requirement then someone can help you better.

  • VoltesDev Profile Picture
    on at

    Hi Mohit,

    Yes, for TableInvoice, I only need InvoiceId and CustAccount, then I would join to CustTrans based on these two information.

    The two information are correctly selected if I checked in debug window (local variable TableInvoice), but CustTrans all is null, while in the 1st query without the use of grouping, give me the correct record as well.

    Thanks

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    One way is to add while select on TableInvoice matching with custAccount and inside the loop add select statement to find firstonly record for CustTrans.

    You can check this article, I think fetching single record in child table is not straight forward in select statement.

    dev.goshoom.net/.../

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 545 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 408

#3
Adis Profile Picture

Adis 267 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans