I need to achieve the following SQL statement in X++ when creating my query.
"
select itemid, RANKSALESFREQUENCY, RANKSALESQUANTITY
from TMC_RANKSALESBYWHSE, MCRINVENTTABLEINDEX, INVENTDISTINCTPRODUCT
where MCRINVENTTABLEINDEX.SEARCHTEXT like'%red%'
and TMC_RANKSALESBYWHSE.product = MCRINVENTTABLEINDEX.REFRECID
and TMC_RANKSALESBYWHSE.inventsiteid ='1210'
and INVENTLOCATIONID ='hou'
and INVENTDISTINCTPRODUCT.product = TMC_RANKSALESBYWHSE.PRODUCT
orderby RANKSALESQUANTITY desc
"
The above SQL-statement sorts the itemIds retrieved from the full text search in the RankSalesQuantity descending order.
However when I try to create that in X++, the sort is not applied to the overall result. It is as if it is only applied to TMC_RankSalesByWhse.
Below is my code
****
Query queryLocal = new Query();
QueryBuildDataSource qbds, qbds2;
QueryBuildRange qbr;
vSearchText = strReplace(_mcrSearchText, '%', '*');
qbds = queryLocal.addDataSource(tableNum(MCRInventTableIndex));
qbr = qbds.addRange(fieldNum(MCRInventTableIndex, searchText));
if (mcrFullTextParameters.SearchType == MCRSearchMatchType::Full)
{
qbr.rangeType(QueryRangeType::FullText);
qbr.value(SysQuery::value(vSearchText));
}
else
{
// If the user added their own wild card do not wrap the search text
if (strFind(vSearchText, '*', 1, strLen(vSearchText)))
{
qbr.value(vSearchText);
}
else
{
qbr.value(SysQuery::valueLike(vSearchText));
}
}
qbds2 = SysQuery::findOrCreateDataSource(queryLocal, tableNum(TMC_RankSalesByWhse), tableNum(MCRInventTableIndex));
qbds2.joinmode(JoinMode::InnerJoin);
qbds2.relations(false);
qbds2.addLink(fieldnum(MCRInventTableIndex, RefRecId), fieldnum(TMC_RankSalesByWhse, Product));
SysQuery::findOrCreateRange(qbds2, fieldNum(TMC_RankSalesByWhse, InventSiteId)).value(salesTableSite);
SysQuery::findOrCreateRange(qbds2, fieldNum(TMC_RankSalesByWhse, InventLocationId)).value(salesTableWhse);
qbds2.AddSortField(fieldNum(TMC_RankSalesByWhse, RankSalesQuantity), SortOrder::Descending);
****
It doesn't want to show me the query generated from the above, only the 2 pieces.
qbds:
{SELECT * FROM MCRInventTableIndex(MCRInventTableIndex_1) WHERE ((SearchText LIKE N'*red*'))}
qbds2:
{SELECT * FROM TMC_RankSalesByWhse(TMC_RankSalesByWhse_1) ORDER BY TMC_RankSalesByWhse.RankSalesQuantity DESC WHERE MCRInventTableIndex.RefRecId = TMC_RankSalesByWhse.Product AND ((InventSiteId = N'1210')) AND ((InventLocationId = N'HOU'))}
queryLocal = {Query object f71e3a00}
This statement is supposed to give me the string: str showQ = QueryRun.query().DataSourceNo(1).toString();
However I get: "SELECT * FROM MCRInventTableIndex(MCRInventTableIndex_1) WHERE ((SearchText LIKE N'*red*'))"
So, is there something wrong with my query?
The only other way I can see how to do it and hopefully the orderDesk will have permission to run an SQL statement, and it is to build the actual SQL statement like in class and method: PriceDiscAdmSearch.reNumberLines and PriceDiscAdmSearch.executeStatement(sqlStmt)