不循环计算总计和小计? [英] Calculate total and subtotal without loop?

查看:23
本文介绍了不循环计算总计和小计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习新的 abap.但我有问题.我想在不使用LOOP"和AT"语句的情况下输出如下结果.

I am starting learning the new abap. But i have problems. I want to make result output as below without using "LOOP" and "AT" statements.

我有像这样的内部表:

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

我需要将输出显示为:

Category Amount
    AAA       10
    AAA       20
    SUBTOTAL  30
    BBB       30
    SUBTOTAL  30
    CCC       40
    CCC       50
    CCC       60
    SUBTOTAL  150
    TOTAL     210

有人可以帮忙吗?

推荐答案

如果您的问题是关于如何使用构造函数表达式 (ABAP >= 7.40) 构建内部表(在内存中),而不是将其呈现在屏幕上或在假脱机文件中(总计和小计是很好地集成在 ALV 中且易于使用的功能),那么这里有一种方法(ASSERT 在这里显示最终值与预期一致):

If your question is about how to build an internal table (in memory) with constructor expressions (ABAP >= 7.40), rather than rendering it on the screen or in a spool file (totals and subtotals are features well integrated in ALV and easy to use), then here's one way to do it (ASSERT is here to show that the final value is as expected) :

TYPES : BEGIN OF ty_line,
          category TYPE string,
          amount   TYPE decfloat16,
        END OF ty_line,
        ty_lines TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.

DATA(gt_main) = VALUE ty_lines( ( 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(lt_display) = VALUE ty_lines(
        ( LINES OF VALUE #(
              FOR GROUPS <g> OF <line> IN gt_main
              GROUP BY ( category = <line>-category )
              ( LINES OF VALUE #( FOR <line2> IN GROUP <g> ( <line2> ) ) )
              ( category = 'SUBTOTAL'
                amount = REDUCE #( INIT subtotal TYPE ty_line-amount
                                   FOR <line2> IN GROUP <g>
                                   NEXT subtotal = subtotal + <line2>-amount ) ) ) )
        ( category = 'TOTAL'
          amount = REDUCE #( INIT total TYPE ty_line-amount
                             FOR <line> IN gt_main
                             NEXT total = total + <line>-amount ) ) ).

ASSERT lt_display = VALUE ty_lines( ( category = 'AAA'      amount = 10 )
                                    ( category = 'AAA'      amount = 20 )
                                    ( category = 'SUBTOTAL' amount = 30 )
                                    ( category = 'BBB'      amount = 30 )
                                    ( category = 'SUBTOTAL' amount = 30 )
                                    ( category = 'CCC'      amount = 40 )
                                    ( category = 'CCC'      amount = 50 )
                                    ( category = 'CCC'      amount = 60 )
                                    ( category = 'SUBTOTAL' amount = 150 )
                                    ( category = 'TOTAL'    amount = 210 ) ).

这篇关于不循环计算总计和小计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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