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 :
Microsoft Dynamics AX (Archived)

Unable to fetch data for other columns when using group by clause

(0) ShareShare
ReportReport
Posted on by 110

Hi All,

I have a scenario where in i need to get the distinct record from a table. If i use 'group by' clause, then i need to select the columns which i'm using in the group by clause. 

Ex--

Table Table1;

while select Voucher, RecId from Table1 group by Table1 .Voucher
{

}

I need to group by only on Vouchers and not with RecId. But if i dont pass RecId to group by clause, the query doesn't gives the expected results. My requirement is to get the RecId and Voucher from the query on grouping by vouchers only. How do i achieve this?

*This post is locked for comments

I have the same question (0)
  • Rustem Galiamov Profile Picture
    8,072 on at

    Hi manasa133!

    If there are several rows with one voucher num and different recId? In this case what you expect?

  • manasa133 Profile Picture
    110 on at

    I expect one record with one voucher number, along with the RecId of that record

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Try this:

    Table1 t1, t2;
        
    while select t1
        group by Voucher
    {
        select firstOnly RecId from t2
            where t2.Voucher == t1.Voucher;
            
        info(strFmt("%1 - %2", t2.Voucher, t2.RecId));
    }


  • Suggested answer
    Mea_ Profile Picture
    60,286 on at

    You cannot group by 1 field and select 2, it's not an AX issue, it just does not make sense in general. However, if you have 2 records with same voucher and you want to get 1 recid and don't really care first or second you can do something like:

    while select Voucher, MAXOF(RecId) from Table1

    group by Table1 .Voucher

    {

    }

    This approach is better than 2 selects because you doing only one, that is obviously faster.

  • Suggested answer
    manasa133 Profile Picture
    110 on at

    Table1 t1, t2;

    while select t1

       group by Voucher

    {

       select firstOnly Voucher, RecId from t2

           where t2.Voucher == t1.Voucher;

       info(strFmt("%1 - %2", t2.Voucher, t2.RecId));

    }

    This works! Thanks :)

  • Rustem Galiamov Profile Picture
    8,072 on at

    And ievgen suggestion also works

    Table1 t1;
    while select maxOf(recid) from t1 group by Voucher { info(strFmt("%1 - %2", t1.Voucher, t1.RecId)); }

    And as ievgen mentioned, this approach is better than 2 selects because you doing only one, that is obviously faster

  • Keshav Kumar Profile Picture
    220 on at

    Hi Manasa

    Whenever you are using group by ,use aggregate functions such as max of, min of, sum etc. in your code.Use below code to get your results.

    Table table1

    while select max of(RecId),voucher from Table1 group by Table1 .Voucher

    {

    }

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans