Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP help with deeply nested items.

Former Member
0 Likes
1,076

Gurus,

We have a requirement whence we need to sum up the key figures from one transformation layer going to the next. The challenge is, its a deeply nested structure. something like :

SalesOrderItem Higher Level Item KeyFig

1000 0000 30

1010 1000 20

1011 1010 10

1015 1010 20

1020 1010 20

1021 1020 10

1022 1020 50

1023 1020 10

1025 1020 20

So now, in the next layer we will be rolling it upto the 1000 , 2000...levels- in this example 1000 level only.

The round figure items(1010, 1020.....) are the individual kmats and the summation happens like this:

1. Items 1021, 1022, 1023 and 1025 roll up to 1020; sum = 90.

2. Now when 1020 rolls up to its higher level item which is 1010, the sum that rolls up from 1020 should be 90 + 20.

3. Ultimately when all items roll up to item 1000, the total sum will be 190.

Can someone please help me write this logic in ABAP?

Thanks!

Chris

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,018

You can try this, but please put it through more examples. Assumption is that your highest level item will have higher item number as 0000 and that they are always sorted in the way it is given in your example.


DATA: BEGIN OF itab OCCURS 0,
        posnr(6) TYPE n,
        uposn(6) TYPE n,
        qty TYPE p DECIMALS 2.
DATA: END OF itab.

DATA: itab2 LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: itab3 LIKE itab OCCURS 0 WITH HEADER LINE.

DATA: v_total TYPE p DECIMALS 2,
      v_posnr(6) TYPE n.

itab-posnr = '1000'.
itab-uposn = '0000'.
itab-qty   = 30.
APPEND itab.

itab-posnr = '1010'.
itab-uposn = '1000'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1011'.
itab-uposn = '1010'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1015'.
itab-uposn = '1010'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1020'.
itab-uposn = '1010'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1021'.
itab-uposn = '1020'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1022'.
itab-uposn = '1020'.
itab-qty   = 50.
APPEND itab.

itab-posnr = '1023'.
itab-uposn = '1020'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1025'.
itab-uposn = '1020'.
itab-qty   = 20.
APPEND itab.
itab2[] = itab[].
*-- assumption is that if UPOSN = 000000, then there is no higher level
*   item
LOOP AT itab WHERE uposn = '000000'.
  itab3-posnr = v_posnr = itab-posnr.
  DO.
    LOOP AT itab2 WHERE uposn = v_posnr.
      v_total = v_total + itab2-qty.
    ENDLOOP.
    IF sy-subrc <> 0.
      EXIT.
    ELSE.
      v_posnr = itab2-posnr.
    ENDIF.
*-- This item does not appear as a higher level item
  ENDDO.
  v_total = v_total + itab-qty.
  itab3-qty = v_total.
  APPEND itab3.
  CLEAR itab3.
ENDLOOP.

LOOP AT itab3.
  WRITE:/ itab3-posnr,
          itab3-qty.
ENDLOOP.

7 REPLIES 7
Read only

Former Member
0 Likes
1,018

i would create a new itab like this:

HL = Higher Level

SO = Sales Order

IT = Item

SUM = Summed values

HL SO IT SUM

1020 1025 20 ___

1020 1023 10 ___

1020 1022 50 ___

1020 1021 10 ___

1010 1020 20 ___

1010 1015 20 ___

1010 1011 10 ___

1000 1010 20 ___

0000 1000 30 ___

basically, swap columns 1 & 2, reverse sort it and add a new SUM column.

loop at itab.
  v_tabix = sy-tabix.  " <- keep track of your current line.

  at end of col1.
    sum.
    read table itab into work_area index sy-tabix.
    work_area-sum = itab-sum.
    modify itab index v_tabix from work_area transporting sum. 
  endat.

endloop.

all this code is theoretical, probably wouldn't pass a syntax check and doesn't make the best use of work areas/field symbols, but it's a good start.

reward points if helpful.

rp.

Read only

0 Likes
1,018

EDIT: the read statement in the loop is incorrect.

loop at itab.
  v_tabix = sy-tabix.  " <- keep track of your current line.
 
  at end of col1.
    sum.
    read table itab into work_area with key SO = itab-HL.
" check sy-subrc to make sure a higher level is found 
    work_area-sum = itab-sum.
    modify itab index v_tabix from work_area transporting sum. 
  endat.
 
endloop.

Read only

0 Likes
1,018

Thank you Robert for your earnest reply.

When you do the sum at end of col1 after the sorting you suggested, we will have two distinct sums for 1020 and 1010. The problem with that is, 1020 has its own keyfigures which need to be added to the sum of which 1020 is a high level item for.

Also,SO Item 1020 has a higher level item 1010, so the combined sum so far has to be added to the sum for which 1010 is the higher level item.

All help appreciated.

Read only

0 Likes
1,018

Chris,

I really think this can be accomplished using that code example as a base.

Create your table so that it has more than 1 summation field so you can roll your lower level numbers to the higher level then add that to it's own value. then, move that to a 3rd field.

Otherwise, you could have a 2nd itab just to store the top level sales orders. They should be easy to find because those are in the Higher Level column of your itab.

Loop through those:

Loop at HL_itab.
  loop at SO_itab where HL = HL_itab-entry.
    add your values here and store somewhere
  endloop.
endloop.

Read only

0 Likes
1,018

Thank you Robert, I ll build more upon your pseudo code. Points assigned for your help:)

Read only

Former Member
0 Likes
1,019

You can try this, but please put it through more examples. Assumption is that your highest level item will have higher item number as 0000 and that they are always sorted in the way it is given in your example.


DATA: BEGIN OF itab OCCURS 0,
        posnr(6) TYPE n,
        uposn(6) TYPE n,
        qty TYPE p DECIMALS 2.
DATA: END OF itab.

DATA: itab2 LIKE itab OCCURS 0 WITH HEADER LINE.
DATA: itab3 LIKE itab OCCURS 0 WITH HEADER LINE.

DATA: v_total TYPE p DECIMALS 2,
      v_posnr(6) TYPE n.

itab-posnr = '1000'.
itab-uposn = '0000'.
itab-qty   = 30.
APPEND itab.

itab-posnr = '1010'.
itab-uposn = '1000'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1011'.
itab-uposn = '1010'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1015'.
itab-uposn = '1010'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1020'.
itab-uposn = '1010'.
itab-qty   = 20.
APPEND itab.

itab-posnr = '1021'.
itab-uposn = '1020'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1022'.
itab-uposn = '1020'.
itab-qty   = 50.
APPEND itab.

itab-posnr = '1023'.
itab-uposn = '1020'.
itab-qty   = 10.
APPEND itab.

itab-posnr = '1025'.
itab-uposn = '1020'.
itab-qty   = 20.
APPEND itab.
itab2[] = itab[].
*-- assumption is that if UPOSN = 000000, then there is no higher level
*   item
LOOP AT itab WHERE uposn = '000000'.
  itab3-posnr = v_posnr = itab-posnr.
  DO.
    LOOP AT itab2 WHERE uposn = v_posnr.
      v_total = v_total + itab2-qty.
    ENDLOOP.
    IF sy-subrc <> 0.
      EXIT.
    ELSE.
      v_posnr = itab2-posnr.
    ENDIF.
*-- This item does not appear as a higher level item
  ENDDO.
  v_total = v_total + itab-qty.
  itab3-qty = v_total.
  APPEND itab3.
  CLEAR itab3.
ENDLOOP.

LOOP AT itab3.
  WRITE:/ itab3-posnr,
          itab3-qty.
ENDLOOP.

Read only

0 Likes
1,018

Thank you Srinivas.

I ll try your code with more examples and let you know the outcome. It does make sense and seems it ll suffice.

Thanks a lot! points assigned for your timely help.