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

Notifications

Announcements

Community site session details

Community site session details

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

Group by in x++

(4) ShareShare
ReportReport
Posted on by 1,979
Hi,

I'm getting this error for this group by code

When using qualified field names, statements can contain a maximum of one 'order by' clause and one 'group by' clause. When using unqualified field names, statements can either contain multiple 'order by' clauses or multiple 'group by' clauses

What's wrong with it?
 

while select FieldA, FieldB from TableA
        group by TableA.FieldA, TableA.FieldB
        where TableA.FieldA == "123"
            && TableA.FieldB == "ABC"
    join sum(FieldC) from TableB 
        where TableB.RefId == TableA.RecId
    join RecId from TableC
        where TableC.LinkId == TableA.LinkId
    join ReferenceField from TableD
        group by TableD.ReferenceField  //if i comment this out the error goes but i need it
        where TableD.LinkId == TableC.LinkId
{
   info("TEST");
}
 
Categories:
I have the same question (0)
  • Verified answer
    Jonas "Jones" Melgaard Profile Picture
    4,991 Most Valuable Professional on at
    "Group by" and "Order By" can only be used once in an select statement, if you use the full table name, and should be defined after the "Join" or "From" clause and before the first "Where" clause if any.
     
    So the select should look something like this:
     
    while select FieldA, FieldB from TableA
            group by TableA.FieldA, TableA.FieldB, TableD.ReferenceField 
            where TableA.FieldA == "123"
                && TableA.FieldB == "ABC"
        join sum(FieldC) from TableB 
            where TableB.RefId == TableA.RecId
        join RecId from TableC
            where TableC.LinkId == TableA.LinkId
        join ReferenceField from TableD
            where TableD.LinkId == TableC.LinkId
    {
       info("TEST");
    }
    Edit: Put emphasis on the syntax is incorrect, if you use tableName as part of you groupBy clause. Details of "should" recommendation is part of the linked docs article.
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,445 Super User 2025 Season 2 on at
    Hi,
     
    Recommendation would be to use the below mentioned syntax. Correcting suggestion 1. 
     
     
    or 
     
     
    Hope this helps. Happy to answer questions, if any.
     
     
     
  • Verified answer
    Martin Dráb Profile Picture
    238,281 Most Valuable Professional on at
    The statement Group by" and "Order By" can only be used once in an select statement is contradicted by the very first sentence of the linked section of documentation: A query can have multiple group by clauses [...]. As the both the error message and the documentioned mentioned, it depends on where the field names are qualified (e.g. group by tableA.FieldA) or not (e.g. group by FieldA).
     
    Let me quote the documentation in full:
     
    A query can have multiple group by clauses, but the fields can be qualified by a table name in only one group by clause. We recommend that you use table name qualifiers. The order by clause follows the same syntax patterns as group by. Both clauses, if they are provided, must appear after the join (or from) clause, and both must appear before any where clause that exists on the same join clause. We recommend that all group by, order by, and where clauses appear immediately after the last join clause. The following example shows a group by clause where a field is qualified by a table name.
     
    This is a variant with a single group by clause and qualified names:
    select salesLine
        group by salesLine.ItemId, salesTable.CustAccount
        join salesTable
            where salesTable.SalesId == salesLine.SalesId; 
    And here multiple group by clauses with unqualified names:
    select salesLine
        group by ItemId
        join salesTable
            group by CustAccount
            where salesTable.SalesId == salesLine.SalesId; 
    The first code snippet suggested by Navneeth has the same bug as the original one. The second one is possible, nevertheless the usual approach is putting the group by clause on the top.
     
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,445 Super User 2025 Season 2 on at
    Thanks Jonas for correcting the response. Will edit the first recommendation so that there are no wrong answers to the set of question in the forum.
     
    Re-wrote the group by and this piece works too. 
  • .. Profile Picture
    1,979 on at
    Hi All,

    Thanks alot for your inputs 

    So the recommendation is, if we want to group by more than one table, then leave the group by till the last join, right? and not under each table

    So it would look like this now:
     
    while select FieldA, FieldB from TableA
            group by TableA.FieldA, TableA.FieldB, TableD.ReferenceField 
            where TableA.FieldA == "123"
                && TableA.FieldB == "ABC"
        join sum(FieldC) from TableB 
            where TableB.RefId == TableA.RecId
        join RecId from TableC
            where TableC.LinkId == TableA.LinkId
        join ReferenceField from TableD
            where TableD.LinkId == TableC.LinkId
    {
       info("TEST");
    }

    Naveenth you mentioned that i should group by RecId as well in Table C but i think this is wrong as it will ruin the whole group by concept 
    I think i can join to RecId without affecting the group by

    so the above code is correct
  • Navneeth Nagrajan Profile Picture
    2,445 Super User 2025 Season 2 on at
    Hi,
     
    Its not necessary to leave the group by till the last join.
     
    You can write it like this, as suggested by Jonas. This will work leaving out the RecId.  
    while select FieldA, FieldB from TableA
            group by TableA.FieldA, TableA.FieldB, TableD.ReferenceField 
            where TableA.FieldA == "123"
                && TableA.FieldB == "ABC"
        join sum(FieldC) from TableB 
            where TableB.RefId == TableA.RecId
        join RecId from TableC
            where TableC.LinkId == TableA.LinkId
        join ReferenceField from TableD
            where TableD.LinkId == TableC.LinkId
    {
       info("TEST");
    }
     
    Usually prefer writing the group by in a single line to reduce the number of lines of code. The usual approach, as Martin highlighted is to put the group by statement on the top but the second option does work well too.

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

News and Announcements

Season of Giving Solutions is Here!

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 843 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 357 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans