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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

QueryRun with GroupBy and sorting

(0) ShareShare
ReportReport
Posted on by

Hi All,

I'm working on a query of 3 related table: InventSum, InventDim, and InventBatch. I need to sum the Available Qty in InventSum with grouping InventDim, at the same time fetch the data in FEFO sequence.

After apply SUM selection and sortfields for grouping, I'm trying to sort the last table as primary sorting. 

I found that once I have set the table OrderMode with GroupBy, I could not add sorting or set the primary sorting field. And if I don't group the related table, the data is not loaded.

Refer my code below

static void KZ_QueryRunTest(Args _args)
{


Query querySum;
QueryRun queryRunSum;
QueryBuildDataSource qDBSum,qDBDim,qDBBatch;
QueryBuildRange queryRange;
InventSum inventSum;
InventDim inventDim;
InventBatch inventBatch;
InventDimParm InventDimParm1,InventDimParm2;
ItemId itemId='M~Test_Batch';
InventLocationId inventLocationId = '01';
InventSiteId inventSiteId = '01';
;

querySum = new Query();
qDBSum = querySum.addDataSource(tablenum(InventSum));
qDBDim = qDBSum.addDataSource(tablenum(InventDim));
qDBDim.joinMode(JoinMode::InnerJoin);
qDBDim.relations(true);
qDBBatch = qDBDim.addDataSource(tablenum(InventBatch));
qDBBatch.joinMode(JoinMode::InnerJoin);
qDBBatch.relations(true);

queryRange = qDBSum.addRange(fieldnum(InventSum,ItemId));
queryRange.value(queryValue(itemId));
queryRange = qDBDim.addRange(fieldnum(InventDim,InventSiteId));
queryRange.value(queryValue(inventSiteId));
queryRange = qDBDim.addRange(fieldnum(InventDim,InventLocationId));
queryRange.value(queryValue(inventLocationId));

InventDimParm1.initValue();
InventDim.initValue(); InventDim.InventSiteId = inventSiteId; InventDim.inventLocationId = inventLocationId;
InventSum::queryAddSumFields(qDBSum);
InventDim::queryDatasourceAddRangeSortFromParms(qDBDim,false,InventDim::findOrCreate(InventDim),InventDimParm1,InventDimParm::activeDimFlag('SWLB'));
qDBDim.addSortField(fieldnum(InventDim,InventBatchId));
qDBDim.orderMode(OrderMode::GroupBy);
qDBBatch.addSortField(fieldnum(InventBatch,ExpDate));
qDBBatch.addOrderByField(fieldnum(InventBatch,ExpDate));
qDBBatch.orderMode(OrderMode::GroupBy);

queryRunSum = new QueryRun(querySum);

//queryRunSum.run();return;

While (queryRunSum.next())
{
InventSum = queryRunSum.get(tablenum(InventSum));
inventDim = queryRunSum.get(tablenum(InventDim));
InventBatch = queryRunSum.get(tablenum(InventBatch));
info(strfmt('Batch: %1, ExpDate: %2',InventDim.inventBatchId,InventBatch.expDate));
}

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    238,143 Most Valuable Professional on at

    That's expected - you can't combine grouping and sorting. Therefore you'll have to look for a workaround, such doing grouping in a view and sorting the view.

  • Community Member Profile Picture
    on at

    Seems like I have to go without sum the InventSum's fields

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

News and Announcements

Season of Giving Solutions is Here!

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

#2
TAHER Mehdi Profile Picture

TAHER Mehdi 3

#3
Nakul Profile Picture

Nakul 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans