Skip to main content

Notifications

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,

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,554 Super User 2024 Season 1 on at
    RE: A bit help on X++ select query

    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/.../

  • VoltesDev Profile Picture
    VoltesDev on at
    RE: A bit help on X++ select query

    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
    Mohit Rampal 12,554 Super User 2024 Season 1 on at
    RE: A bit help on X++ select query

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,996 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,853 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans