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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

AX query - sorting with sum field

(0) ShareShare
ReportReport
Posted on by 436

Hi all,

I am trying to sort by a sumation of field, but its not working.
My sql query is working okay, but X++ not 
SQL - 

select  sum(NETAMOUNT),CUSTACCOUNT
from RETAILTRANSACTIONTABLE
group by CUSTACCOUNT
order by sum(NETAMOUNT) desc

AX query -

            qbds.addGroupByField(fieldNum(RetailTransactionTable, CustAccount));   
            qbds.addSelectionField(fieldNum(RetailTransactionTable,netAmount),SelectionField::Sum);  
            qbds.addSortField(fieldNum(RetailTransactionTable,netAmount),SortOrder::Descending);


Suggest me how to achieve this.

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    udaY-ch Profile Picture
    5,133 on at

    Hi,

    Can you show us the full query. Hope you had done it well.

    axspecialist.wordpress.com/.../query-with-aggregate-function

    Best,

    Uday.

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

    Hi Sona Jee!

    Try this:

    qbds.addGroupByField(fieldNum(RetailTransactionTable, CustAccount));   
    qbds.addGroupByField(fieldNum(RetailTransactionTable, netAmount),SelectionField::Sum);  
    qbds.addSortField(fieldNum(RetailTransactionTable, netAmount),SortOrder::Descending);


    i.e, change addSelectionField to addGroupByField

  • Rustem Galiamov Profile Picture
    8,072 on at

    Ahh..Sorry. You need one total sum to every customer.

  • Sona Jee Profile Picture
    436 on at

    Hi Rustem Galiamov,

    I tried with addGroupByField , it didn't worked.

    Please suggest me another option.

  • Rustem Galiamov Profile Picture
    8,072 on at

    This query for lookup or something else?

  • Sona Jee Profile Picture
    436 on at

    This query for filter top customer account based on sum of netAmount and order by descending.

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

    I think the best way is to create a view with customers and total netAmount by customer and then use this view in your query.

  • Keshav Kumar Profile Picture
    220 on at

    Hi

    I think your sequence is incorrect.

    Try this

    qbds.addSelectionField(fieldNum(RetailTransactionTable,netAmount),SelectionField::Sum);  

    qbds.addGroupByField(fieldNum(RetailTransactionTable, CustAccount));  

    qbds.addSortField(fieldNum(RetailTransactionTable,netAmount),SortOrder::Descending);

  • Sona Jee Profile Picture
    436 on at

    Hi Keshav Kumar

    It didn't worked.

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

    You can't do it like in SQL. Try to create view as i mentioned earlier.

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

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

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans