1
votes

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?

1
What is the difference between this and your previous question? stackoverflow.com/questions/22246734/… - Jan B. Kjeldsen

1 Answers

2
votes

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.