How to 'group by' using variables in 4gl

≯℡__Kan透↙ 提交于 2019-12-02 19:07:46

问题


Is there a way to group records by a field within a table within a 4gl query?

My code.

  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.

Thank you


回答1:


Yes, of course you can, very basic:

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.



回答2:


Actually this is what the ACCUMULATE STATEMENT and the ACCUM function are meant for. I find the exact syntax for those somewhat hard to remember (and sometimes difficult to understand even after reading the online help), so I often resort to accumulating the variables myself like in Jensd's answer. The code for your example should look something like this (syntax-checked but not tested):

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.


来源:https://stackoverflow.com/questions/54476465/how-to-group-by-using-variables-in-4gl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!