2017-08-07 59 views
0

我有一個BQL查詢連接三個表如下:如何聚合一個領域BQL複雜查詢

foreach (PXResult<GLTran, Branch, xTACOpenSourceDetail> rec in 
           PXSelectJoin<GLTran, 
              InnerJoin<Branch, 
               On<GLTran.branchID, Equal<Branch.branchID>>, 
              InnerJoin<xTACOpenSourceDetail, 
               On<Branch.branchCD, Equal<xTACOpenSourceDetail.string03>, 
               And<xTACOpenSourceDetail.openSourceName, Equal<Constants.openSourceName>, 
               And<xTACOpenSourceDetail.dataID, Equal<Constants.privateer>>>>>>, 
             Where<Branch.branchCD, NotEqual<Required<Branch.branchCD>>, 
             And<GLTran.posted, Equal<True>, 
             And<GLTran.ledgerID, Equal<Required<GLTran.ledgerID>>, 
             And<GLTran.tranDate, GreaterEqual<Required<GLTran.tranDate>>>>>>, 
             OrderBy<Asc<xTACOpenSourceDetail.string01, Asc<GLTran.batchNbr>>>>.Select(Base, osdBranch.String03, ledger.LedgerID, tacsmlm.Date01)) 

我想補充一個聚合的領域,即通過GLTran分組的的GLTran.CuryDebitAmt的總和.BatNbr和Branch.BranchCD。

我可以用SUM OVER的功能如下這樣做很容易在SQL:

SELECT SUM(GLTran.CuryDebitAmt) OVER (PARTITION BY GLTran.BatchNbr, Branch.BranchCD) as 'BatchTotal' 
     ,GLTran.* 
     ,Branch.* 
     ,xTACOpenSourceDetail.* 

FROM GLTran 
     Inner Join Branch 
      On GLTran.branchID = Branch.branchID 
      AND Branch.CompanyID = GLTran.CompanyID 
     Inner Join xTACOpenSourceDetail 
      On Branch.branchCD = xTACOpenSourceDetail.string03 
      And xTACOpenSourceDetail.openSourceName = 'TAC FM Map Company Branch' 
      And xTACOpenSourceDetail.dataID = 'Privateer' 
      AND xTACOpenSourceDetail.CompanyID = GLTran.CompanyID 


Where Branch.branchCD <> '000 0000' 
     And GLTran.posted = 1 
     And GLTran.ledgerID = 6 
     And GLTran.tranDate >= '08/03/2017' 
     AND GLTran.CompanyID = 2 

Order 
By  xTACOpenSourceDetail.string01 ASC 
     ,GLTran.batchNbr ASC 

...但我不知道如何在BQL添加這一個總結領域。任何幫助表示讚賞。

回答

1

您將使用PXSelectGroupBy,並且在您的BQL的Aggreate中指示哪些字段將「SUM」其值。任何未被調用的字段將是最大值。

如果您在Acumatica源中搜索SUM<,您可以找到大量的BQL示例。這是一個來自ARPaymentEntry的BQL示例。只有兩個(curyAdjdAmt & adjAmt)字段將包含一個SUM,而返回的所有其他字段將是MAX。

SOAdjust other = PXSelectGroupBy<SOAdjust, 
    Where<SOAdjust.voided, Equal<False>, 
     And<SOAdjust.adjdOrderType, Equal<Required<SOAdjust.adjdOrderType>>, 
     And<SOAdjust.adjdOrderNbr, Equal<Required<SOAdjust.adjdOrderNbr>>, 
     And< 
      Where<SOAdjust.adjgDocType, NotEqual<Required<SOAdjust.adjgDocType>>, 
      Or<SOAdjust.adjgRefNbr, NotEqual<Required<SOAdjust.adjgRefNbr>>>>>>>>, 
    Aggregate<GroupBy<SOAdjust.adjdOrderType, 
     GroupBy<SOAdjust.adjdOrderNbr, 
     Sum<SOAdjust.curyAdjdAmt, 
     Sum<SOAdjust.adjAmt>>>>>>.Select(this, adj.AdjdOrderType, adj.AdjdOrderNbr, adj.AdjgDocType, adj.AdjgRefNbr); 

你的問題的另一種替代的解決方案是創建一個PXProjection這可能是由組值然後您可以在你的常規選擇包括投影表VS在您的BQL基表的總和。我不知道其中一個或另一個的性能優勢 - 只是另一種選擇。

+0

謝謝,Brendan ..; D – pmfith