Advance ranges example in axapta
SysQuery query;
SysQueryRun queryRun;
QueryBuildDataSource custInvoiceJourDS, custInvoiceTransDS;
;
query = new Query();
custInvoiceJourDS = query.addDataSource(tablenum(CustInvoiceJour));
custInvoiceJourDS.addRange(fieldnum(CustInvoiceJour, InvoiceAccount));
custInvoiceJourDS.addRange(fieldnum(CustInvoiceJour, CurrencyCode));
custInvoiceTransDS = custInvoiceJourDS.addDataSource(tablenum(CustInvoiceTrans));
custInvoiceTransDS.addRange(fieldnum(CustInvoiceTrans, ItemId));
custInvoiceTransDS.relations(true);
queryRun = new SysQueryRun(query);
queryRun.prompt();