我有一个原始数据集,如下所示:
我需要转换为今天的样子,即 2024 年 12 月 18 日:
本质上是在旋转。 到目前为止,我设法使用 RTTS 创建输出表的动态表类型,现在需要找到一种方法从原始数据填充它。
基本逻辑是基于日期、工厂和土地的数量汇总:
如果 wadat = erdat,_t0 列是当天的总和,
_older 是当天的总和,如果 erdat 比当前日期早一天以上
ABC 是 ZUKRL 中所有存在 ABC 的一天的总和
到目前为止,我只能根据简单的分组进行求和:
t_data = VALUE #( FOR GROUPS g1 OF ls_row IN gt_data GROUP BY ( wadat = ls_row-wadat
werks = ls_row-werks
land1 = ls_row-land1
"erdat = ls_row-erdat
)
LET sum = REDUCE #( INIT line TYPE ty_sum_line
FOR members_g1 IN GROUP g1
NEXT line-wadat = members_g1-wadat
line-werks = members_g1-werks
line-land1 = members_g1-land1
line-lfimg = line-lfimg + members_g1-lfimg
)
IN ( wadat = g1-wadat
werks = g1-werks
land1 = g1-land1
lfimg = sum-lfimg
"lfimg_t0
"lfimg_older
"lfimg_abc
)
).
我的想法是用适当的总和填充 lfimg_t0 lfimg_older lfimg_abc,然后循环并填充动态表。请您帮我解决一下分组逻辑。
添加评论后进行澄清: 如何从组 g_tot 中获取值: “lfimg_t0 “lfimg_older “lfimg_abc
t_data2 = VALUE #( FOR GROUPS g1 OF ls_row IN gt_data GROUP BY ( wadat = ls_row-wadat
werks = ls_row-werks
land1 = ls_row-land1
)
FOR GROUPS g_tot OF row_tot IN gt_data GROUP BY ( wadat = row_tot-wadat
werks = row_tot-werks
)
LET sum = REDUCE #( INIT line TYPE ty_sum_line
FOR members_g1 IN GROUP g1
NEXT line-wadat = members_g1-wadat
line-werks = members_g1-werks
line-land1 = members_g1-land1
line-lfimg = line-lfimg + members_g1-lfimg
)
IN ( wadat = g1-wadat
werks = g1-werks
land1 = g1-land1
lfimg = sum-lfimg
"lfimg_t0
"lfimg_older
"lfimg_abc
)
).
...我想我需要一个字段来存储这来自哪个组,以便能够填充结果表,因为有许多不同的 WERKS 甚至更多 LAND1
也许你错过了
LINES OF
。
注意,代码相当复杂,如果将其拆分为方法会更容易理解。
DATA(intermediate_lines) = VALUE intermediate_lines(
FOR GROUPS <group_by_wadat_werks> OF <source_line> IN source_lines
GROUP BY
( wadat = <source_line>-wadat
werks = <source_line>-werks )
LET sums_by_wadat_werks_land1 = VALUE intermediate_lines(
( LINES OF VALUE #(
FOR GROUPS <group_by_land1>
OF <source_line_2> IN GROUP <group_by_wadat_werks>
GROUP BY
( land1 = <source_line_2>-land1 )
LET sums = REDUCE sums(
INIT sums_2 TYPE sums
FOR <line_of_group_by_land1> IN GROUP <group_by_land1>
NEXT sums_2-lfimg = sums_2-lfimg
+ <line_of_group_by_land1>-lfimg )
IN
( wadat = <group_by_wadat_werks>-wadat
werks = <group_by_wadat_werks>-werks
land1 = <group_by_land1>-land1
lfimg = sums-lfimg ) ) ) )
sums_by_wadat_werks = VALUE intermediate_line(
LET sums_3 = REDUCE sums( INIT sums_2 TYPE sums
FOR <line_sums_by_wadat_werks_lan> IN sums_by_wadat_werks_land1
NEXT sums_2-lfimg = sums_2-lfimg
+ <line_sums_by_wadat_werks_lan>-lfimg )
IN wadat = <group_by_wadat_werks>-wadat
werks = <group_by_wadat_werks>-werks
land1 = 'TOT'
lfimg = sums_3-lfimg )
IN
( LINES OF sums_by_wadat_werks_land1 )
( sums_by_wadat_werks ) ).
在上面的块之前编写代码,以测试它:
DATA(source_lines) = VALUE source_lines( werks = 'DE10'
lfimg = 1
( wadat = '20241217' land1 = 'DE' )
( wadat = '20241217' land1 = 'DE' )
( wadat = '20241217' land1 = 'FR' )
( wadat = '20241218' land1 = 'DE' ) ).
在上面的代码块之后编写代码,以检查结果(这是预期的结果):
ASSERT intermediate_lines = VALUE intermediate_lines(
werks = 'DE10'
( wadat = '20241217' land1 = 'DE' lfimg = 2 )
( wadat = '20241217' land1 = 'FR' lfimg = 1 )
( wadat = '20241217' land1 = 'TOT' lfimg = 3 )
( wadat = '20241218' land1 = 'DE' lfimg = 1 )
( wadat = '20241218' land1 = 'TOT' lfimg = 1 ) ).
顶级声明:
TYPES lfimg TYPE p LENGTH 7 DECIMALS 2.
TYPES:
BEGIN OF source_line,
wadat TYPE d,
werks TYPE c LENGTH 4,
land1 TYPE land1,
lfimg TYPE lfimg,
END OF source_line.
TYPES source_lines TYPE STANDARD TABLE OF source_line WITH EMPTY KEY.
TYPES:
BEGIN OF intermediate_line,
wadat TYPE d,
werks TYPE c LENGTH 4,
land1 TYPE land1,
lfimg TYPE lfimg,
END OF intermediate_line.
TYPES intermediate_lines TYPE STANDARD TABLE OF intermediate_line WITH EMPTY KEY.
TYPES:
BEGIN OF sums,
lfimg TYPE lfimg,
END OF sums.