要获得新的aap中的总和小计而无循环 [英] To get total and subtotal without loop in new abap
本文介绍了要获得新的aap中的总和小计而无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始学习新的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 ) ).
这篇关于要获得新的aap中的总和小计而无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文