Application Development 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: 

Query on Internal Table

Former Member
0 Kudos

Hi Guys,

My ITAB looks like below.


PN      SN        MATNR     BRGEW    VOLUM    
1         11           D1           100             10
1         11           D1           150             25  
1         11           D2            50              10
2         11           F1           100             10
2         11           F2           150             25  
2         11           F2            50              10    

My requirement is for same PN & SN if MATNR is same ADD BRGEW and VOLUM along with the details of PN SN MATNR BRGEW VOLUM and store it in another internal table.

My final Table should look like below


PN      SN        MATNR     BRGEW    VOLUM    
1         11           D1           250             35
1         11           D2            50              10
2         11           F1           100             10
2         11           F2           200             35  

Thanks in Advance,

Prasad.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Use:

Collect Statement

Itab --->Key Fields --> PN ,SN, MATNR(Declare these as key Fileds of itab)
Itab2

Sort itab.
Loop at Itab.
Collect Itab to Itab1.
Endloop.

ITAB1 Will give you required output.

Regards,

Gurpreet

9 REPLIES 9

Former Member
0 Kudos

Use:

Collect Statement

Itab --->Key Fields --> PN ,SN, MATNR(Declare these as key Fileds of itab)
Itab2

Sort itab.
Loop at Itab.
Collect Itab to Itab1.
Endloop.

ITAB1 Will give you required output.

Regards,

Gurpreet

0 Kudos

Guys can you give it in brief.

My Internal tables have work area.

Thanks,

Prasad.

0 Kudos

Hi,

Test the following code here i am using Work Area in fact when you create internal table with OCCURS it automatically create an Work Area with the Same name as internal table and when you use WITH HEADER LINE it do the same, you can't do loop on internal table with out work area.

Let me know if any problem,

LOOP AT it into wa_it_final.
  COLLECT wa_it_final into it_final.
ENDLOOP.

Kind Regards,

Faisal

0 Kudos

Hi Faisal ,

My problem here is


LOOP AT it into wa_it_final.
  COLLECT wa_it_final into it_final.
ENDLOOP.

my wa_it_final has 4 fileds.

But my It_final has 3 fileds.

Its saying that the work area and the Internal table is incompatible.

How to do this.

0 Kudos

Hi,

Test the following Sample Code this time it will solve out your problem. use MOVE-CORRESPONDING

DATA: BEGIN OF it OCCURS 10,
  m(10),
  amount1 TYPE i,
  amount2 TYPE i,
  END OF it.

DATA: BEGIN OF it_final OCCURS 10,
  m(10),
  amount1 TYPE i,
  END OF it_final.

it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'BBB'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'BBB'. it-amount1 = 10. it-amount2 = 20. APPEND it.

SORT it BY m.

LOOP AT it into it.
  MOVE-CORRESPONDING it to it_final.
  COLLECT it_final into it_final.
ENDLOOP.

LOOP AT it_final.
  WRITE: / it_final-m, it_final-amount1.
ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

0 Kudos

Hi, Dheeru

Use the WorkArea of 4 field when do loop and inside the loop body MOVE-CORRESPONDING field of 4 Field workarea to 3 Field work area remember one think that the final table where you are going to collect the Values must be of 3 Field like your WorkArea of 3 Field,

Waiting for your Reply,

Kind Regards,

Faisal

0 Kudos

Thanks a Ton Faisal.

My issue is resolved.

Points awarded.

faisal_altaf2
Active Contributor
0 Kudos

Hi,

Test the following Sample Code Hope will Help you to solve out your problem,

DATA: BEGIN OF it OCCURS 10,
  m(10),
  amount1 TYPE i,
  amount2 TYPE i,
  END OF it.

DATA: it_final LIKE STANDARD TABLE OF it WITH HEADER LINE.

it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'BBB'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'AAA'. it-amount1 = 10. it-amount2 = 20. APPEND it.
it-m = 'BBB'. it-amount1 = 10. it-amount2 = 20. APPEND it.

LOOP AT it.
  COLLECT it INTO it_final.
ENDLOOP.

LOOP AT it_final.
  WRITE: / it_final-m, it_final-amount1, it_final-amount2.
ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

former_member203501
Active Contributor
0 Kudos

look at this example....

REPORT ztest .

data: begin of itab occurs 0,

i type i,

j type i,

k type i,

end of itab.

data: begin of itab1 occurs 0,

k type i,

end of itab1.

itab-i = 1.

itab-j = 1.

itab-k = 2.

append itab.

itab-i = 1.

itab-j = 1.

itab-k = 4.

append itab.

itab-i = 1.

itab-j = 2.

itab-k = 6.

append itab.

itab-i = 1.

itab-j = 3.

itab-k = 8.

append itab.

loop at itab .

if itab-i = itab-j.

itab1-k = itab-k.

append itab1.

endif.

write:/ itab-i, itab-j, itab-k.

endloop.

uline.

loop at itab1.

write:/ itab1-k.

endloop.