首先,我对Progress 4GL非常新,并且仍然试图了解FOR EACH块的嵌套是如何工作的。我有以下两个表,我从中获取信息,ivc_header和ivc_mchgs:
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,primary是由invoice_nbr,line_nbr和misc_seq_nbr组成的复合键。外键是invoice_nbr。
只是关于数据的说明,ivc_mchgs中的信息是发票line_nbr的杂项费用。
我想要得到的是invoice_to_cust_nbr + sold_to_cust seq的invoice_amt和扩展总额。在做了一些研究之后,我决定将总数放在变量中,而不是使用ACC'内置的ACCUMULATE函数。
以下是我的代码:
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 :(得分: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 ...
等
答案 1 :(得分: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.
优雅方式是使用左外连接组合两个查询,并使用ACCUMULATE函数,但这应该有效。