如何 'group by' 在 4gl 中使用变量

How to 'group by' using variables in 4gl

有没有办法在 4gl 查询中按 table 中的字段对记录进行分组?

我的代码。

  define variable v-invoice     as inte    no-undo.
define variable v-sell-price  as decimal no-undo.
define variable v-cost-price  as decimal no-undo. 
define variable iinv          as integer no-undo.

For each Order no-lock :

v-invoice      = Order.tblinvoice.
v-sell-price   = Order.sell-price.
v-cost-price   = Order.cost-price. 
iinv           = iinv + Order.sell-price.

   display Order.invoice Order.sell-price.
end.

谢谢

是的,当然可以,非常基础:

DEFINE VARIABLE v-sell-price AS INTEGER NO-UNDO.
DEFINE VARIABLE v-cost-price AS INTEGER NO-UNDO.

FOR EACH order BREAK BY order.invoice:

    /* Sum the prices */
    ASSIGN 
        v-sell-price = v-sell-price + Order.sell-price.
        v-cost-price = v-cost-price + Order.cost-price. 

    /* On the last order matching this order display and reset sums */
    IF LAST-OF(order.invoice) THEN DO:

        DISPLAY Order.invoice v-sell-price v-cost-price.
        ASSIGN 
            v-sell-price = 0
            v-cost-price = 0.
    END.
END.

实际上,这就是 ACCUMULATE STATEMENT 和 ACCUM 函数的用途。 我发现那些有点难以记住的确切语法(有时甚至在阅读在线帮助后也难以理解),所以我经常像 Jensd 的回答那样自己积累变量。 您的示例代码应如下所示(已检查语法但未测试):

FOR EACH order BREAK BY order.invoice:

    accumulate Order.sell-price (sub-total by order.invoice).
    accumulate Order.cost-price (sub-total by order.invoice).

    IF LAST-OF(order.invoice) THEN

        DISPLAY Order.invoice 
          accum sub-total by order.invoice Order.sell-price
          accum sub-total by order.invoice Order.cost-price.
END.