2016-09-15 77 views
0

首先我對進程4GL非常陌生,仍然試圖探索如何嵌套FOR EACH塊。我有以下兩個表我得到的信息出來,ivc_header和ivc_mchgs:進度4GL嵌套塊顯示相關數據

ivc_header 
    invoice_nbr  | sold_to_cust_nbr | sold_to_cust_seq | invoice_amt 
     1000051  |  70  |  0   | $1,000 
     1000049  |  70  |  1   | $1,500 
     1000010  |  310  |  0   | $2,000 
     1000011  |  310  |  1   | $2,500 

ivc_mchgs 
    invoice_nbr | line_nbr | misc_seq_nbr | extension 
     1000051 |  1 |  1  | $300 
     1000051 |  1 |  2  | $200 
     1000051 |  2 |  1  | $100 
     1000049 |  1 |  1  | $400 
     1000049 |  1 |  2  | $100 
     1000049 |  2 |  1  | $150 
     1000010 |  1 |  1  | $50 
     1000010 |  1 |  2  | $50 
     1000010 |  2 |  1  | $100 
     1000011 |  1 |  1  | $75 
     1000011 |  1 |  2  | $80 
     1000011 |  2 |  1  | $90 

僅供參考,爲ivc_header主鍵是invoice_nbr和ivc_mchgs主要是由invoice_nbr,line_nbr的組合鍵,和misc_seq_nbr。外鍵是invoice_nbr。

只需注意一下數據,ivc_mchgs中的信息就是發票line_nbr的雜項費用。

我想要得到的是總的invoice_amt和由sold_to_cust_nbr + sold_to_cust seq擴展。在做了一些研究後,我決定把總數放在變量中,而不是使用ACCUMULATE函數中的Progress。

這裏是我的代碼:

DEFINE VARIABLE cCustNum AS CHARACTER   NO-UNDO. 
DEFINE VARIABLE dInvoiceSubTotal AS DECIMAL  NO-UNDO. 
DEFINE VARIABLE dSurchargeTotal AS DECIMAL  NO-UNDO. 

FOR EACH ivc_header 
    NO-LOCK 
    WHERE (ivc_header.sold_to_cust_nbr = "000070") 
      OR (ivc_header.sold_to_cust_nbr = "000310") 
    BREAK BY ivc_header.sold_to_cust_nbr: 
    IF FIRST-OF(ivc_header.sold_to_cust_nbr) THEN 
     ASSIGN dInvoiceSubTotal = 0. 
     ASSIGN dInvoiceSUbTotal = dInvoiceSUbTotal + ivc_header.invoice_amt. 
    IF LAST-OF(ivc_header.sold_to_cust_nbr) THEN 
     DISPLAY ivc_header.sold_to_cust_nbr + ivc_header.sold_to_cust_seq FORMAT "x(9)" LABEL "CustNum" 
      dInvoiceSUbTotal LABEL "SubTotal". 
FOR EACH ivc_mchgs WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr 
    NO-LOCK 
    BREAK BY ivc_mchgs.invoice_nbr: 
    IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN 
     ASSIGN dSurchargeTotal = 0. 
     ASSIGN dSurchargeTotal = dSurchargeTotal + ivc_mchgs.extension. 
    IF LAST-OF (ivc_mchgs.invoice_nbr) THEN 
     DISPLAY 
       dSurchargeTotal LABEL "Surcharge". 
    END. 
END. 

此代碼將使我通過sold_to_cust_nbr + sold_to_cust_seq總invoice_amt和彙總由invoice_nbr擴展。我無法弄清楚如何做的是通過sold_to_cust_nbr + sold_to_cust_seq獲得總計延長。

任何幫助表示讚賞。

謝謝

回答

0

你想要的假設都是由發票和總結交流總,那麼你可以只是做這樣的:

DEFINE VARIABLE cCustNum AS CHARACTER   NO-UNDO. 
DEFINE VARIABLE dInvoiceSubTotal AS DECIMAL  NO-UNDO. 
DEFINE VARIABLE dSurchargeTotal AS DECIMAL  NO-UNDO. 
DEFINE VARIABLE dSurchargeSubTl AS DECIMAL  NO-UNDO. 

FOR EACH ivc_header 
    NO-LOCK 
    WHERE (ivc_header.sold_to_cust_nbr = "000070") 
      OR (ivc_header.sold_to_cust_nbr = "000310") 
    BREAK BY ivc_header.sold_to_cust_nbr: 
    IF FIRST-OF(ivc_header.sold_to_cust_nbr) THEN 
     ASSIGN dInvoiceSubTotal = 0 
       dSurchargeSubTl = 0. 
    ASSIGN dInvoiceSUbTotal = dInvoiceSUbTotal + ivc_header.invoice_amt. 
    IF LAST-OF(ivc_header.sold_to_cust_nbr) THEN 
     DISPLAY ivc_header.sold_to_cust_nbr + ivc_header.sold_to_cust_seq FORMAT "x(9)" LABEL "CustNum" 
      dInvoiceSUbTotal LABEL "SubTotal" 
      dSurchargeSubTL LABEL "Srchg SubTl". 
    FOR EACH ivc_mchgs WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr 
     NO-LOCK 
     BREAK BY ivc_mchgs.invoice_nbr: 
     IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN 
      ASSIGN dSurchargeTotal = 0. 
     ASSIGN dSurchargeTotal = dSurchargeTotal + ivc_mchgs.extension. 
     IF LAST-OF (ivc_mchgs.invoice_nbr) THEN DO: 
      DISPLAY dSurchargeTotal LABEL "Surcharge". 
      ASSIGN dSurchargeSubTl = dSurchargeSubTl + dSurchargeTotal. 
     END. 
    END. 
END. 

優雅方式是使用左外連接到這兩個查詢組合,並使用ACCUMU LATE函數,但這應該工作。

2

我想你可能不知道你可以指定多個BY子句。

督察你可能要內FOR EACH像這樣的代碼:

FOR EACH ivc_mchgs NO-LOCK WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr 
    BREAK BY ivc_mchgs.invoice_nbr 
      BY ivc_mchgs.sold_to_cust_nbr 
      BY ivc_mchgs.sold_to_cust_seq: 

    IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN 
     ASSIGN dSurchargeTotal = 0. 

    IF FIRST-OF(ivc_mchgs.sold_to_cust_nbr) THEN ... 

+0

感謝您對多個BY子句的建議,您是對的,我不知道。 – CuriousOne