gpt4 book ai didi

abap - 不用循环计算总计和小计?

转载 作者:行者123 更新时间:2023-12-04 01:51:37 27 4
gpt4 key购买 nike

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

我有这样的内部表:

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 来表明最终值符合预期):

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

关于abap - 不用循环计算总计和小计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com