I want to get this query working in axapta
Select Count(Distinct CUSTACCOUNT), YEARMONTH
From ALT_CUSTOMERSALES As AC
Where INVOICEDATE >= DateAdd(Day, -90, GetDate())
Group By YEARMONTH
Where ALT_CUSTOMERSALES
is a view that relates SalesTable
and CustInvoiceJour
. I'm searching for sales to unique customers per month. I tried using the clause existjoin between the view and CustTable
, but the group by isn't working. Any suggestions?
This is the code I have:
Query query = new Query('miQuery');
QueryBuildDataSource vsQBDS, custTaQBDS;
QueryBuildRange qbr1;
QueryRun qr;
months = 3;
dPeriod = DateTimeUtil::date(DateTimeUtil::addDays(DateTimeUtil::utcNow(), months * -30));
custTaQBDS = query.addDataSource(tableNum(CustTable));
custTaQBDS.addSelectionField(fieldNum(CustTable, AccountNum), SelectionField::Count);
vsQBDS = custTaQBDS.addDataSource(tableNum(ALT_CustomerSales));
vsQBDS.fields().addField(fieldNum(ALT_CustomerSales, YearMonth));
vsQBDS.relations(false);
vsQBDS.joinMode(JoinMode::ExistsJoin);
vsQBDS.addLink(fieldNum(ALT_CustomerSales, CustAccount), fieldNum(CustTable, AccountNum));
qbr1 = vsQBDS.addRange(fieldNum(ALT_CustomerSales, InvoiceDate));
qbr1.value(strFmt("(%2 > %1)", date2StrXpp(dPeriod), fieldStr(ALT_CustomerSales, InvoiceDate)));
//vsQBDS.addGroupByField(fieldNum(ALT_CustomerSales, YearMonth));
qr = new QueryRun(query);