我有以下代码在Req Trans表上构建查询,以按项目/站点显示合并数量;
query = new Query();
dsReqTrans = query.addDataSource(tablenum(ReqTrans));
dsReqTrans.addRange(fieldnum(ReqTrans, ReqPlanId)).value(_reqPlanId);
dsReqTrans.addRange(fieldnum(ReqTrans, RefType)).value(enum2str(_reqRefType));
dsReqTrans.addGroupByField(fieldnum(ReqTrans, ItemId));
dsReqTrans.addSelectionField(fieldnum(ReqTrans, Qty), SelectionField::Sum);
dsInventDim = dsReqTrans.addDataSource(tablenum(InventDim));
dsInventDim.joinMode(JoinMode::InnerJoin);
dsInventDim.relations(false);
dsInventDim.addLink(fieldnum(ReqTrans, CovInventDimId), fieldnum(InventDim, InventDimId));
dsInventDim.addGroupByField(fieldnum(InventDim, InventSizeId));
dsInventDim.addGroupByField(fieldnum(InventDim, InventSiteId));
rangeInventSiteId = dsInventDim.addRange(fieldnum(InventDim, InventSiteId));
rangeInventSiteId.value(_parmSiteId);
这非常有效。我按项目/网站获得了qty列的总和。
但是,如果我还要在ItemGroupId上添加范围。为此,我需要像这样链接到InventTable;
dsInventTable = dsReqTrans.addDataSource(tablenum(InventTable));
dsInventTable.joinMode(JoinMode::InnerJoin);
dsInventTable.relations(false);
dsInventTable.addLink(fieldnum(ReqTrans, ItemId), fieldnum(InventTable, ItemId));
dsInventTable.addRange(fieldnum(InventTable, ItemGroupId)).value(_parmItemGroupId);
添加此数据源时,InventDim的链接已损坏。我获得了按项目分组的所有网站的所有项目数量的总和,而不是按项目/网站分组。
为什么会这样?
答案 0 :(得分:3)
基本上你不能有两个内连接到同一个数据源(你可以,但它不起作用)。
如果使用AX 2012,您可以将InventTable
数据源添加到InventDim
,而不是:
dsInventTable = dsInventDim.addDataSource(tablenum(InventTable));
dsInventTable.joinMode(JoinMode::ExistsJoin);
dsInventTable.relations(false);
// OBS 3 arguments next:
dsInventTable.addLink(fieldnum(ReqTrans, ItemId), fieldnum(InventTable, ItemId), dsReqTrans.name());
dsInventTable.addRange(fieldnum(InventTable, ItemGroupId)).value(_parmItemGroupId);
在早期版本中,您首先放置InventTable
(请记住按InventTable
中的一个字段分组):
query = new Query();
dsInventTable = dsReqTrans.addDataSource(tablenum(InventTable));
dsInventTable.addGroupByField(fieldnum(InventTable, ItemId)); // You have to group/sum something
dsReqTrans = dsInventTable.addDataSource(tablenum(ReqTrans));
dsReqTrans.joinMode(JoinMode::InnerJoin);
dsReqTrans.relations(false);
dsReqTrans.addLink(fieldnum(InventTable, ItemId).fieldnum(ReqTrans, ItemId));
...