RE: AX query - sorting with sum field
Hi,
I have tried the same in AX 2012 and job looks like,
static void job1(Args _args)
{
Query query = new Query();
QueryRun queryRun;
QueryBuildDataSource qbdsRetailTransaction;
RetailTransactionTable retailTransactionTable;
qbdsRetailTransaction = query.addDataSource(tableNum(RetailTransactionTable));
qbdsRetailTransaction.addGroupByField(fieldNum(RetailTransactionTable, InvoiceId));
qbdsRetailTransaction.addSelectionField(fieldNum(RetailTransactionTable,netAmount),SelectionField::Sum);
qbdsRetailTransaction.addSortField(fieldNum(RetailTransactionTable,netAmount),SortOrder::Descending);
queryRun = new QueryRun(query);
while (queryRun.next())
{
retailTransactionTable = queryRun.get(tableNum(RetailTransactionTable));
info(strFmt("Invoiceid %1 -- Net amount %2",retailTransactionTable.invoiceId,retailTransactionTable.netAmount));
}
}
The query seems to be same as what you got to see in the SQL
SELECT SUM(netAmount) FROM RetailTransactionTable(RetailTransactionTable_1) GROUP BY RetailTransactionTable.invoiceId ORDER BY RetailTransactionTable.netAmount DESC
I have used InvoiceId instead of customer. I