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

Itab manipulation

Former Member
0 Likes
1,793

Hi all,

I have an itab filled with the following values.

This itab contains quantities of a particular material(100) sold to different lands in a period of time

The user enters a period from Oct2009 - Mar2010.

Sorted my itab1 by matr land month.

MatNr Land  month  quantity

100    A    01.10     5
100    A    02.10     4
100    A    03.10     5
100    A    09.10     5
100    B    12.09     5
100    C    11.09     4

For the user entered range of Oct2009 - Mar2010

These months are calculated and stored in a structure below:

TYPES : BEGIN OF gty_monthcalc,
          month TYPE  c length 30,
        END OF gty_monthcalc.

DATA gt_monthcalc TYPE TABLE OF gty_monthcalc.
DATA gs_monthcalc LIKE LINE OF gt_monthcalc.

In my example the range was between 
03.11.2009 - 05.03.2010.

So the gt_monthcalc contains the following  months.

11.09
12.09
01.10
02.10
03.10

So Now I have to go through the itab1 and write the quantities for each land in the order(Nov to march) into a string and use the "write" statement to write the string out.

Now the problem Iam facing here is the months which are there in gt_monthcalc itab and which are not there in the itab1 for each land I have to add a new line to the itab and write it out.

For example:-
100    A    11.10     0
100    A    12.10     0

and for other lands as well which doesnt contain the months in gt_monthcalc itab.

What is the optimal way to do it....

Thanks

P

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,757

Strange I've got this one


100                A 10.09             0,00
100                A 11.09             0,00
100                A 12.09             0,00
100                A 01.10             5,00
100                A 02.10             4,00
100                A 03.10             5,00
100                A 09.10             5,00
100                B 10.09             0,00
100                B 11.09             0,00
100                B 12.09             5,00
100                B 01.10             0,00
100                B 02.10             0,00
100                B 03.10             0,00
100                C 10.09             0,00
100                C 11.09             4,00
100                C 12.09             0,00
100                C 01.10             0,00
100                C 02.10             0,00
100                C 03.10             0,00

Are you using this sort just before output? For correct table?

Regards

Marcin

14 REPLIES 14
Read only

MarcinPciak
Active Contributor
0 Likes
1,757

Try this one



TYPES:BEGIN OF t_itab,
      matnr TYPE matnr,
      land  TYPE c,
      month(5) TYPE c,
      qua TYPE p DECIMALS 2,
     END OF t_itab.

TYPES: tt_itab TYPE TABLE OF t_itab WITH KEY matnr land month.

DATA: itab TYPE tt_itab WITH HEADER LINE.
DATA: itab_fill TYPE tt_itab WITH HEADER LINE.

itab-matnr = '100'.
itab-land = 'A'.
itab-month = '01.10'.
itab-qua = 5.
APPEND itab.

itab-matnr = '100'.
itab-land = 'A'.
itab-month = '02.10'.
itab-qua = 4.
APPEND itab.

itab-matnr = '100'.
itab-land = 'A'.
itab-month = '03.10'.
itab-qua = 5.
APPEND itab.

itab-matnr = '100'.
itab-land = 'A'.
itab-month = '09.10'.
itab-qua = 5.
APPEND itab.

itab-matnr = '100'.
itab-land = 'B'.
itab-month = '12.09'.
itab-qua = 5.
APPEND itab.

itab-matnr = '100'.
itab-land = 'C'.
itab-month = '11.09'.
itab-qua = 4.
APPEND itab.

DATA: BEGIN OF gt_monthcalc OCCURS 0,
          month TYPE  c LENGTH 30,
        END OF gt_monthcalc.


gt_monthcalc-month = '11.09'.
APPEND gt_monthcalc.
gt_monthcalc-month = '12.09'.
APPEND gt_monthcalc.
gt_monthcalc-month = '01.10'.
APPEND gt_monthcalc.
gt_monthcalc-month = '02.10'.
APPEND gt_monthcalc.
gt_monthcalc-month = '03.10'.
APPEND gt_monthcalc.


SORT itab BY matnr land month.

LOOP AT itab.
  AT END OF land.
    LOOP AT gt_monthcalc.
      READ TABLE itab WITH KEY matnr = itab-matnr
                               land  = itab-land
                               month = gt_monthcalc-month
                               TRANSPORTING NO FIELDS.
      IF sy-subrc <> 0.
        itab_fill-matnr = itab-matnr.
        itab_fill-land = itab-land.
        itab_fill-month = gt_monthcalc-month.
        itab_fill-qua = 0.
        APPEND itab_fill.
      ENDIF.
    ENDLOOP.
  ENDAT.
ENDLOOP.

APPEND LINES OF itab_fill TO itab.
SORT itab BY matnr land month.

LOOP AT itab.
  WRITE: / itab-matnr, itab-land, itab-month, itab-qua.
ENDLOOP.

Regards

Marcin

Read only

0 Likes
1,757

Marcin,

I just ran the code.

The output is not right

Land "A" already has these quantities filled in

100    A    01.10     5
100    A    02.10     4
100    A    03.10     5
100    A    09.10     5

The quantity for month which are not there in gt_monthcalc itab for Land "A" is oct,nov and december.so there should be 3 for lines added to itab for land A

like along with the existing ones.....

100    A    10.09     0
100    A    11.09     0
100    A    12.09     0

This must be true for other land as well. The months which are not there in the itab should be added with 0 quantity...

Read only

0 Likes
1,757

Apparently there is missing one entry for OCT in gt_monthcalc.

Simply append this month


gt_monthcalc-month = '10.09'.
APPEND gt_monthcalc.

and you will get all months for each land from 10.09 - 03.10

Regards

Marcin

Read only

Former Member
0 Likes
1,757

Roughly & ugly (of course don't use heade lines...):

DATA :itab2 TYPE table of ty_itab1,
 itab3 TYPE TABLE of ty_itab1.

SORT itab1 BY matnr land period.
itab2[] = itab1[].
DELETE ADJACENT DUPLICATES FROM itab2 COMPARING matnr land.
LOOP AT itab1.
  LOOP at gt_month.
    READ TABLE itab1 TRANSPORTING NO-FIELDS
       WITH KEY matnr = itab1-matnr
                       land = itab1-land
                         period = gt_month-period BINARY search.
    IF sy-subrc = 0.
     "hurray already in
      CONTINUE.
     ELSE.
      " no sales...
     itab3-land = itab1-land.
     itab3-matnr = itab1-matnr.
     itab3-period = gt_months-period.
     itab3-sales = 0.
     APPEND itab3.
  ENDIF.
 ENDLOOP.
ENDLOOP.
IF sy-subrc = 0.
APPEND LINES OF itab3 TO itab1.
FREE: itab2, itab3.
ENDIF.

Then you have all (& still too much) data in itab1.

the rest shoul be clear...

EDIT: two great minds think alike

Read only

0 Likes
1,757

Axel,

The output from the code which u gave is as follows:

100                A 01.10             5,00
100                A 02.10             4,00
100                A 03.10             5,00
100                A 09.10             5,00
100                A 11.09             0,00
100                A 11.09             0,00
100                A 11.09             0,00
100                A 11.09             0,00
100                A 12.09             0,00
100                A 12.09             0,00
100                A 12.09             0,00
100                A 12.09             0,00
100                B 01.10             0,00
100                B 02.10             0,00
100                B 03.10             0,00
100                B 11.09             0,00
100                B 12.09             5,00
100                C 01.10             0,00
100                C 02.10             0,00
100                C 03.10             0,00
100                C 11.09             4,00
100                C 12.09             0,00

2 questions.

for the months which are not there atleast for "A" it is repeated thrice with 0 quantities.

should I have to delete it with "delete adjacent entries" or what is the reason for that...

Guys,

What should I do to sort the land by month so that it always goes from lower range to higher(From Nov- March)

Edited by: pazzuzu on Mar 17, 2010 4:57 PM

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,757

Hello,

I have used Marcin's code as a reference & modified it :


DATA ITAB1 TYPE TT_ITAB WITH HEADER LINE.

SORT ITAB BY MATNR LAND MONTH.

ITAB1[] = ITAB[].

DELETE ADJACENT DUPLICATES FROM ITAB1 COMPARING MATNR LAND.

LOOP AT GT_MONTHCALC.
  LOOP AT ITAB1.
    MOVE-CORRESPONDING ITAB1 TO ITAB_FILL.
    ITAB_FILL-MONTH = GT_MONTHCALC-MONTH.
    CLEAR ITAB_FILL-QUA.
    APPEND ITAB_FILL.
  ENDLOOP.
ENDLOOP.

SORT ITAB_FILL BY MATNR LAND MONTH.

LOOP AT ITAB_FILL.
  READ TABLE ITAB WITH KEY
  MATNR = ITAB_FILL-MATNR
  LAND  = ITAB_FILL-LAND
  MONTH = ITAB_FILL-MONTH BINARY SEARCH.
  IF SY-SUBRC EQ 0.
    ITAB_FILL-QUA = ITAB-QUA.
    MODIFY ITAB_FILL.
  ENDIF.
ENDLOOP.

LOOP AT ITAB_FILL.
  WRITE: / ITAB_FILL-MATNR, ITAB_FILL-LAND,
           ITAB_FILL-MONTH, ITAB_FILL-QUA.
ENDLOOP.

The output looks something like this:

100 A 01.10 5,00

100 A 02.10 4,00

100 A 03.10 5,00

100 A 10.09 0,00

100 A 11.09 0,00

100 A 12.09 0,00

100 B 01.10 0,00

100 B 02.10 0,00

100 B 03.10 0,00

100 B 10.09 0,00

100 B 11.09 0,00

100 B 12.09 5,00

100 C 01.10 0,00

100 C 02.10 0,00

100 C 03.10 0,00

100 C 10.09 0,00

100 C 11.09 4,00

100 C 12.09 0,00

BR,

Suhas

Read only

0 Likes
1,757

Thank you very much Guys....

It works perfect.

1 last question.

What should be done to sort the itab accoring to matnr,Land and month so that

the month is displayed from lowerrange to upper range (That is from Nov2009 -Mar 2010).Should I have to change the type of month to date from string.

Read only

0 Likes
1,757

Hi,

You no need to change the type.

Check this,

SORT itab by matnr land ascending month descending.

Read only

0 Likes
1,757

What should be done to sort the itab accoring to matnr,Land and month so that

the month is displayed from lowerrange to upper range (That is from Nov2009 -Mar 2010).Should I have to change the type of month to date from string.

Sort it like this

SORT itab BY matnr land month+3(2) month(2).

Regards

Marcin

Read only

0 Likes
1,757

wlll try it out guys...

Edited by: pazzuzu on Mar 18, 2010 10:01 AM

Read only

Former Member
0 Likes
1,757

Hi,

Easiest way is to collect all possible material and Land info in another itab.

Then start to loop in that table. ( Use this table as a key ) gt_mat_land

loop at gt_mat_land.

loop at gt_monthcalc.

clear my_itab1.

read table my itab1 with key matnr = gt_mat_land-matnr

land = gt_mat_land-land

month = gt_monthcalc-month.

if sy-subrc = 0.

move-corresponding my_itab1 to itab_final.

else.

move gt_mat_land-matnr to itab_final-matnr.

move gt_mat_land-land to itab_final-land.

move gt_monthcalc-month to itab_final-month.

endif.

collect itab_final.

endloop.

endloop.

itab_final will have all the vlues with 0 amounts for all mats lands and months. Then you can write or use in ALV.

Read only

MarcinPciak
Active Contributor
0 Likes
1,758

Strange I've got this one


100                A 10.09             0,00
100                A 11.09             0,00
100                A 12.09             0,00
100                A 01.10             5,00
100                A 02.10             4,00
100                A 03.10             5,00
100                A 09.10             5,00
100                B 10.09             0,00
100                B 11.09             0,00
100                B 12.09             5,00
100                B 01.10             0,00
100                B 02.10             0,00
100                B 03.10             0,00
100                C 10.09             0,00
100                C 11.09             4,00
100                C 12.09             0,00
100                C 01.10             0,00
100                C 02.10             0,00
100                C 03.10             0,00

Are you using this sort just before output? For correct table?

Regards

Marcin

Read only

0 Likes
1,757

Marcin sorry,

Was sorting the wrong table.

It works perfectly. Thanks a lot pal.You made my day....

Read only

0 Likes
1,757

Thanks a lot guys......