2014-04-01 78 views
1

我想用addselectionfield選擇4字段。從這4個字段2是總計(總和)字段。 如何使用查詢選擇聚合字段?

我做了以下內容:

  date fromDate = systemDateGet(); 
      date toDate = (systemDateGet() + 25); 
      query   = new Query(); 
      qbds   = query.addDataSource(tableNum(MyTable)); 
      qbds.addSelectionField(fieldNum(MyTable, DateField)); 
      qbds.addSelectionField(fieldNum(MyTable, USDAmountField), SelectionField::Sum); 
      qbds.addSelectionField(fieldNum(MyTable, CurrencyNameField)); 
      qbds.addSelectionField(fieldNum(MyTable, EURAmountField), SelectionField::Sum); 
      qbr   = qbds.addRange(fieldNum(MyTable, DateField)); 
      qbr.value(queryRange(fromDate, toDate)); 

      queryRun  = new QueryRun(query); 

在調試,我得到下面的SELECT語句中queryRun:

SELECT SUM(EURAmountField) FROM MyTable(MyTable_1) 
WHERE ((DateField >= {ts '2014-04-01 00:00:00.000'} 
AND DateField <= {ts'2014-04-26 00:00:00.000'})) 

那麼是什麼原因它只選擇最後一個addSelectionField和我所擁有的要做所有的字段,因爲我已經寫在addSelectionFields?

回答

5

沒有理由(我能想到)...這應該工作,選擇字段應該被添加到您的querybuilddatasource。嘗試同步,編譯和/或重新啓動您的客戶端/ aos。

您還必須做的是,按照您要選擇的字段上的字段添加分組。否則AX將不知道要選擇什麼值,因爲多個記錄可能具有不同的值。例如,選擇CurrencyNameField,將其添加爲一個選擇字段,還可以使用:

qbds.addGroupByField(fieldNum(MyTable, CurrencyNameField)); 

然後你會在CurrencyNameField領域獲得的數據,但你會得到每種貨幣名稱的總和。

這似乎工作:

Query query; 
QueryBuildDataSource qbds; 
QueryBuildRange qbr; 
QueryRun queryrun; 

date fromDate = systemDateGet(); 
date toDate = (systemDateGet() + 25); 
query   = new Query(); 
qbds   = query.addDataSource(tableNum(MyTable)); 
qbds.addGroupByField(fieldNum(MyTable, DateField)); 
qbds.addGroupByField(fieldNum(MyTable, CurrencyNameField)); 
qbds.addSelectionField(fieldNum(MyTable, DateField)); 
qbds.addSelectionField(fieldNum(MyTable, CurrencyNameField)); 
qbds.addSelectionField(fieldNum(MyTable, EURAmountField), SelectionField::Sum); 
qbds.addSelectionField(fieldNum(MyTable, USDAmountField), SelectionField::Sum); 
qbr   = qbds.addRange(fieldNum(MyTable, DateField)); 
qbr.value(queryRange(fromDate, toDate)); 

queryRun  = new QueryRun(query); 

的queryrun包含此:

SELECT DateField, CurrencyNameField, SUM(EURAmountField), SUM(USDAmountField) 
FROM MyTable(MyTable_1) GROUP BY MyTable.DateField, MyTable.CurrencyNameField 
WHERE ((DateField>={ts '2014-04-01 00:00:00.000'} 
AND DateField<={ts '2014-04-26 00:00:00.000'})) 
相關問題