无需 LOOP 即可获得总数 [英] Get total without LOOP

查看:30
本文介绍了无需 LOOP 即可获得总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参考了这个问题和答案

I refer to this question and answers To get total and subtotal without loop in new abap. But my requirements are much simpler than that. I only need the categories with their corresponding totals.

I have internal table like:

Category Amount
AAA     10
AAA     20
BBB     30
CCC     40
CCC     50
CCC     60

I need to build the internal table as this:

Category Amount
AAA       30
BBB       30
CCC       150

Can anyone help with this?

解决方案

From release 7.40 and onward, this can be accomplished by combining grouping in internal tables (FOR GROUPS) with REDUCE for calculating the sum of each group.

First a couple declarations:

TYPES:
   BEGIN OF data_struct,
    category TYPE c LENGTH 3,
    amount TYPE i,
   END OF data_struct,
   data_tab TYPE STANDARD TABLE OF data_struct WITH EMPTY KEY.

DATA(input) = VALUE data_tab(
    ( Category = 'AAA' Amount = 10 )
    ( Category = 'AAA' Amount = 20 )
    ( Category = 'BBB' Amount = 30 )
    ( Category = 'CCC' Amount = 40 )
    ( Category = 'CCC' Amount = 50 )
    ( Category = 'CCC' Amount = 60 )
).

And here the solution:

DATA(output) = VALUE data_tab(
    FOR GROUPS group OF input_line IN input 
        GROUP BY ( category = input_line-category )
        (   category = group-category
            amount = REDUCE #(
                INIT subtotal = 0
                FOR group_line IN GROUP group
                NEXT subtotal  = subtotal  + group_line-amount 
            ) 
        ) 
).

Now what exactly happens here?

The data for the internal table output is generated by FOR GROUPS. The FOR GROUPS table expression consists of three parts: The declaration of the group, the grouping criteria and the results.

First about the declarative part: GROUPS group OF input_line IN input. This defines that there is a group called "group", that its source is the internal table "input" and that we refer to each line of that source table as "input_line".

Next the grouping criteria: GROUP BY ( category = input_line-category ). This means that each group is identified by a key with a single field "category" and that we want to group all lines where the field "category" is identical.

And now for the result part:

( category = group-category
  amount = REDUCE #(
    INIT subtotal = 0
    FOR group_line IN GROUP group
    NEXT subtotal = subtotal + group_line-amount
  )
)

This defines how each group is supposed to be represented in the output set. The first field "category" is simple - just put in the value of "category" of the group called "group".

The second field "amount" is a bit more complicated, because its value is calculated by a REDUCE. The purpose of the REDUCE construction expression is to take many values and turn them into one value. It does this by doing the following things:

  1. Declare and initialize a variable: INIT subtotal = 0
  2. Do a loop over an internal table or the lines of a group in an outer FOR GROUPS. In this case the latter: FOR group_line IN GROUP group
  3. Do the same code snippet with each line of that input, changing the variable. In this case by adding its amount to the subtotal: NEXT subtotal = subtotal + group_line-amount
  4. Return the final value of the variable declared in step 1.

Result from debugger:

这篇关于无需 LOOP 即可获得总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆