AX 2009 Query类(二)

mac2022-06-30  140

二. 多字段时的过滤方式

(1)单一range单一field单一值过滤:

View Code Str exp  =  ‘I - 999 ’;Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId));Qbr.value(exp);或:Qbr.value(queryvalue(‘I - 999 ’));或:Qbr.value(strfmt(‘( % 1   ==  “ % 2 ”)’,fieldstr(InventTable,ItemId),queryvalue(‘I - 999 ’));或:Exp  =  ‘(‘ + fieldid2name(tablenum(inventTable),fieldnum(inventTable,Itemid)) +   '  = " ' +   ' 11 ' + ' /") || ( ' + fieldid2name(tablenum(inventTable),fieldnum(inventTable,itemId)) +   ' == ' + ' "aa" ' + ’)’;Qbr.value(exp);

(2)单一range单一field多值过滤:

    (a) 或关系(假设两个值为value1和value2,类型为字符串):

 

View Code Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(queryvalue(value1));Qbr.value(queryvalue(value2));或:Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(queryvalue(value1,value2));或:Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(strfmt(‘(( % 1   ==  “ % 2 ”)  ||  ( % 1   ==  “ % 3 ”))’,fieldstr(InventTable,ItemId),queryvalue(value1),queryvalue(value2))

 

    (b) 与关系(假设两个值为value1和value2,类型为字符串):

 

Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(strfmt(‘(( % 1   ==  “ % 2 ”)  &&  ( % 1   ==  “ % 3 ”)),fieldstr(InventTable,ItemId),queryvalue(value1),queryvalue(value2));

 

   (c)  取值范围(value1<=value<=value2)

 

Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(queryvalue(value1) + ’..’ + queryvalue(value2));或:Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(strfmt(‘(( % 1   >=  “ % 2 ”)  &&  ( % 1   <=  “ % 3 ”))’,fieldstr(InventTable,ItemId),queryvalue(value1),queryvalue(value2)); (value1 < value < value2)Qbr  =  qbds.addRange(fieldnum(InventTable,ItemId))Qbr.value(strfmt(‘(( % 1   >  “ % 2 ”)  &&  ( % 1   <  “ % 3 ”))’,fieldstr(InventTable,ItemId),queryvalue(value1),queryvalue(value2));

 

 需要注意的是在过滤date型数据时,表达式中不能使用’<=’或’>=’符号。

(3)过滤date型的数据:

 Date2strXpp(date)方法将一个date变量转换为可以在query.value中使用的字符串。Date 类型同样可以使用’>’,’<’或’>=’,’<=’.

 

Str queryExpr   =  ‘(‘  + Fieldid2name(tablenum(SalesLine),fieldnum(SalesLine,ShippingDateRequested))  +  ‘ > ’  +  date2strxpp(tmpDate)  +  ‘)’;Qbr  =  Qbds.addRange(fieldnum(SalesLine,ShippingDateRequested));Qbr.value(strfmt(‘( % 1 . % 2   <   % 3 )’,tablestr(SalesLine),Fieldstr(SalesLine,ShippingDateRequested),Date2strxpp(tmpDate)));        或:Date paymentDateFrom,paymentDateTo;Qbr  =  qbds.addRange(fieldnum(SalesLine,ShippingDateRequested));Qbr.value(queryValue(paymentDateFrom) + ’..’);或:Qbr.value(queryValue(paymentDateFrom) + ’..’ + Queryvalue(paymentDateTo));或:Qbr.value(‘..’ + queryvalue(paymentDateTo));

(4)通配符的使用:

Qbr  =  qbds.addRange(InventTable,ItemId);Qbr.value(strfmt(‘( % 1 . % 2  Like “ % 3 ”)’, qbds.name(),fieldstr(InventTable,ItemId),’I * ’));或:Qbr.value(strfmt(‘( % 1  Like “ % 2 ”)’, fieldstr(InventTable,ItemId),’I * ’));

(5)过滤多元素也即数组型的EDT数据类型:

 在过滤数组型的字段时,可以使用方法fieldId2Ext(fieldId,tableIndex)来添加range,如:

Qbds  =  query.addDateSource(tablenum(CustTable));Qbr  =  qbds.addRange(fieldId2Ext(fieldnum(CustTable,Dimension), 1 ));Qbr.value(strfmt(‘ % 1 ’,dimension[ 1 ]));qbds.addRange(fieldId2Ext(fieldnum(CustTable,Dimension), 2 ));Qbr.value(strfmt(‘ % 1 ’,dimension[ 2 ]));qbds.addRange(fieldId2Ext(fieldnum(CustTable,Dimension), 3 ));Qbr.value(strfmt(‘ % 1 ’,dimension[ 3 ]));

这里%1可以用双引号引起来,也可以不引起来。

(6)单一range多fields过滤

View Code Qbds  =  query.addDataSource(tablenum(SalesTable),’SalesTable’);qbr  =  qbds.addRange(fieldnum(SalesTable,SalesId));qbr.value(strfmt( ' (((%1.%2 == "%3") && (%1.%4 == "%5"))) ' ,qbds.name(), // %1 fieldstr(SalesTable,CustAccount), // %2 " 1101 " , // %3 fieldstr(SalesTable,SalesId), // %4 " SO-100019 " ));或str queryExpr;queryExpr  =   ' (( '   +  tableid2name(tablenum(CustTable)) + ' . ' + fieldid2name(tablenum(CustTable),fieldnum(CustTable,AccountNum))  +   ' ==" ' + queryvalue( " 1101 " +   ' ")&&( '   +  tableid2name(tablenum(CustTable)) + ' . ' + fieldid2name(tablenum(CustTable),fieldnum(CustTable,Name))  +   ' ==" ' + queryvalue( " Forest Wholesales " +   ' ")) ' ;query  =   new  Query();qbds  =  query.addDataSource(tablenum(CustTable), ' CustTable ' );qbr  =  qbds.addRange(fieldnum(CustTable,AccountNum));qbr.value(queryExpr);

 

 在这两种格式下,需要注意一点:在表达是中需要用到:table.field样式,这里的table的名字指所添加的datasource的名字,如果你添加一个custTable表作为datasource,你将该datasource命名为custTable_1,那么,在你的表达是中就需要写成custTable_1.field。为了进一步说明这一点,上边第二个例子可以写成这样:

 

View Code queryExpr  =   ' (( ' + " CustTable_1 " + ' . ' + fieldid2name(tablenum(CustTable),fieldnum(CustTable,AccountNum)) +   ' ==" ' + queryvalue( " 1101 " ) +   ' ")&&( ' +   " CustTable_1 " + ' . ' + fieldid2name(tablenum(CustTable),fieldnum(CustTable,Name)) +   ' ==" ' + queryvalue( " Forest Wholesales " ) +   ' ")) ' ;query  =   new  Query(); qbds  =  query.addDataSource(tablenum(CustTable), ' CustTable_1 ' );qbr  =  qbds.addRange(fieldnum(CustTable,AccountNum));qbr.value(queryExpr);

 

query也可以根据条件排序,Group by或 Order by.

query.dataSourceTable(tablenum(CustTable)).orderMode(OrderMode::Order by)//或Group by

queryBuildDataSource.addSortRange(fieldNum(CustTable,AccountNum),SortMode::Descending);

query.addSelectionField(fieldnum(IWS_InventDimSumView,SumOfPhysicalInvent),SelectionField::Sum); //Sum or AVG

也可以用queryBuildDataSource.joinMode()来设置关联模式。

 

 

转载于:https://www.cnblogs.com/Kurodo/archive/2011/07/16/2108414.html

最新回复(0)