在没有LOOP的情况下获得总计 [英] Get total without LOOP in abap

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

问题描述

我指的是这个问题,并回答获得新abap中没有循环的总数和小计.但是我的要求比这简单得多.我只需要类别及其相应的总数.

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.

我的内部表如下:

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

任何人都可以帮忙吗?

推荐答案

从7.40版开始,可以通过组合内部表中的分组来实现此目的(

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.

首先进行一些声明:

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 )
).

这里是解决方案:

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?

内部表output的数据由FOR GROUPS生成. FOR GROUPS表表达式由三部分组成:组的声明,分组条件和结果.

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.

首先是声明部分:GROUPS group OF input_line IN input.这定义了存在一个称为"group"的组,其源是内部表"input".并且我们将该源表的每一行称为"input_line".

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".

下一步分组标准:GROUP BY ( category = input_line-category ).这意味着每个组由具有单个字段类别"的键来标识.并且我们希望将字段"category"所在的所有行分组.完全相同.

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.

现在是结果部分:

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

这定义了应该如何在输出集中表示每个组.第一字段"category"被称为类别".很简单-只需输入"category"的值即可称为组"的组中的一个.

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".

第二个字段金额"有点复杂,因为它的值是由REDUCE计算的. REDUCE构造表达式的目的是采用许多值并将它们变成一个值.它通过执行以下操作来做到这一点:

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. 声明并初始化变量:INIT subtotal = 0
  2. 在内部表或外部FOR GROUPS中的组的行上循环.在这种情况下,后者为:FOR group_line IN GROUP group
  3. 对该输入的每一行执行相同的代码段,以更改变量.在这种情况下,将其金额添加到小计:NEXT subtotal = subtotal + group_line-amount
  4. 返回步骤1中声明的变量的最终值.
  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.

调试器的结果:

这篇关于在没有LOOP的情况下获得总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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