看到标题立马会想到用递归实现,但是递归不好调试,容易出现错误,下面给出一种简单实现方法。
types: begin of ty_prhi,
posnr like prhi-posnr,
up like prhi-up,
down like prhi-down.
include type ty_wbs.
types: end of ty_prhi.
data: lt_prhi type standard table of ty_prhi,
ls_prhi type ty_prhi.
field-symbols:<fs_prhi> like ls_prhi,
<fs_wbs> like gs_wbs.
"1.0 需要将下层WBS上的预算上上层汇总
" GT_WBS中只保存了本层的需求单预算,未包含下层的需求单预算
lt_wbs[] = gt_wbs[].
sort lt_wbs by pspnr.
select * into corresponding fields of table lt_prhi
from prhi
for all entries in lt_wbs
where posnr = lt_wbs-pspnr.
sort lt_wbs by pspnr.
loop at lt_prhi assigning <fs_prhi>.
read table lt_wbs into ls_wbs
with key pspnr = <fs_prhi>-posnr binary search.
if sy-subrc = 0.
move-corresponding ls_wbs to <fs_prhi>.
endif.
endloop.
"按层次倒序排列,将上层相同的排列在一起
sort lt_prhi by stufe descending up ascending.
"从底层往上层累加
loop at lt_prhi into ls_prhi.
read table lt_wbs assigning <fs_wbs>
with key pspnr = ls_prhi-up
binary search.
if sy-subrc eq 0.
read table lt_wbs into ls_wbs
with key pspnr = ls_prhi-posnr
binary search.
"将当前层加到上层
<fs_wbs>-zgssum = <fs_wbs>-zgssum + ls_wbs-zgssum.
endif.
endloop.