1

I want to count how many records are there in CustTable (group by CustGroup) and sort the number of records in each custgroup descending using query code.

here is my code:

q = new Query();
queryBuildDataSource = q.addDataSource(tableNum(custTable));
queryBuildDataSource.addGroupByField(fieldNum(custTable, CustGroup));
//queryBuildDataSource.addSortField(fieldNum(custTable, count(RecId), SortOrder::Descending));
qr = new QueryRun(q);
while(qr.next())
{
   custTable = qr.get(tableNum(custTable));
   info(strFmt("%1 --- %2", custTable.CustGroup, custTable.RecId));
}

I know that 'count' does not work here... how can I solve it?

AYETY
  • 698
  • 4
  • 23
  • 38
  • What is the difference between this and your previous question? http://stackoverflow.com/questions/22246734/make-order-by-with-query-in-aot – Jan B. Kjeldsen Mar 18 '14 at 11:47

1 Answers1

2

You cannot (in X++) sort on an aggregated field.

What you can do is make a view, then sort on its output explained in this answer and here.

Community
  • 1
  • 1
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50