Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Group by & Order by count(RecId)

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

CustTable custTable;
while select count(RecId), CustGroup from custTable
group by CustGroup
order by custTable.RecId desc
{
info(strFmt("%2 - %1", custTable.RecId, custTable.CustGroup));
}

Hello, I've encountered interesting case in AX 2012. Basically what I am trying to do is to sort my data by the amount that count(RecId) returns when it is grouped by CustGroup. Well, "order by custTable.count(RecId) or any other variation of that arises syntax error. Do you have any idea, what to do with this? 

I was suggested to use View, but even with View, I couldn't find the solution which lets me order by the expression of aggregated function (count in this case).

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Group by & Order by count(RecId)

    That settles everything. I had been trying to put the whole logic either in select statement or in View, together they work perfectly. Thanks for your effort!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Group by & Order by count(RecId)

    Hi!

    I try use a view and it work

    ustTable custTable;
        CustTestView  CustTestView;
        
        while select count(RecId), CustGroup from custTable
            group by CustGroup
            order by custTable.RecId desc
            
        {
            info(strFmt("%2 - %1", custTable.RecId, custTable.CustGroup));
        }
        
        info('View');
        
        while select CustTestView
            order by CountOfRecId desc
        {
                info(strFmt("%2 - %1", CustTestView.CountOfRecId, CustTestView.CustGroup));
        }    


    8321.Test.jpg

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... 292,074 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,900 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans