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

Internal table query

Former Member
0 Likes
1,328

Dear all,

I have an internal table with date ,material,plant and quantity as component.

now i want to combine the quantity of those records with same date same plant and same material into a single record.

How can i do it.

eg

data : begin of itab occurs 0,

budat type mkpf-budat,

matnr type mseg-matnr,

erfmg type mseg-erfmg,

end of itab.

I am having data into the internal table as

date material plant quantity

20.07.2005 2 n001 160

20.07.2005 2 n001 60

20.07.2005 2 n002 80

20.07.2005 2 n002 90

please tell how can i combine the first two rows into one.

Regards

Mave

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,302

Hi Mave

You should use the statament collect instead of append while appending record to ITAB:

SELECT budat matnr erfmg from <table> into itab

where ...

collect itab.

endselect.

Max

11 REPLIES 11
Read only

Former Member
0 Likes
1,302

hi,

try this one

define one more itab like itab_unique

itab_unique[] = itab[].

sort itab_unique by material plant date.

DELETE ADJACENT DUPLICATES from itab_unique comparing material plant date.

cheers,

sasi

Read only

ChristianFi
Active Participant
0 Likes
1,302

loop over the internal table and insert the records into another one with the same structure with the statement collect.

Christian

Read only

Former Member
0 Likes
1,302

HI mave,

i don't understood your problem?

do you want put in a sigle variable (or field of internal table) your differets values?

something like

data t type table of string.

data wa type string.

concatenate itab_tab butad itab-matnr itab_erfmg into wa.

append wa to t.

Read only

Former Member
0 Likes
1,302

when ur going to update the data in database or printing in the list formatting u can do like this.

loop at itab into wa.

concatenate wa-date wa-plant

wa-material into w_key.

on change of w_key.

wl = wa.

endon.

if w_key eq w_key5.

if wr_key ne w_key.

if not wl is initial.

collect wl-quan to itab1-quan.

endif.

collect wa-quan to itab1.

clear wl.

wr_key = w_key.

else.

collect wa-quan to itab1.

endif.

endif.

w_key5 = w_key.

clear w_key.

endloop.

also update the the itab1 tablle and print the same.

hope ur problem will be solved.

Reward points if ur problm is solved.

Read only

0 Likes
1,302

ateeq

Your variables are confusing please define them

regards

mave

Read only

Former Member
0 Likes
1,303

Hi Mave

You should use the statament collect instead of append while appending record to ITAB:

SELECT budat matnr erfmg from <table> into itab

where ...

collect itab.

endselect.

Max

Read only

0 Likes
1,302

you are genious ....very easy solution man

regards

Mave

Read only

Former Member
0 Likes
1,302

Hi,

Try this -

data : begin of itab occurs 0,

budat type mkpf-budat,

matnr type mseg-matnr,

erfmg type mseg-erfmg,

end of itab.

data final like itab occurs 0 with header line.

loop at itab.

final = itab.

collect final.

endloop.

As per your given example, the entries in itab are -

date material plant quantity

20.07.2005 2 n001 160

20.07.2005 2 n001 60

20.07.2005 2 n002 80

20.07.2005 2 n002 90

now in final table with above code, you will get following ntries.

date material plant quantity

20.07.2005 2 n001 220

20.07.2005 2 n002 170

Thanks,

Rajeev

Read only

Former Member
0 Likes
1,302

Hi,

The explanation Sasi has given you is correct.

As Sasi noted don't fotget to <b>sort</b> the internal

table before deleting those records.

Regards,

Siva

Read only

Former Member
0 Likes
1,302

Hi Mave,

1. Create another internal table of the same type, say table-2.

2. Sort the table1 accordingly.

3. Loop the table1.

4. At end of "Sorted Field" , Sum the "Required Field".

5. Assign to the same field.

6. Append the record to Table-2.

7. End loop.

Hope this may be useful for you.

Warm Regards,

Baburaj

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,302

Hi,

data lv_index type sy-tabix.

data : begin of itab1 occurs 0,

budat type mkpf-budat,

matnr type mseg-matnr,

erfmg type mseg-erfmg,

end of itab1.

itab1[] = itab[].

sort itab by date material plant .

loop at itab1.

lv_sum = 0.

lv_index = sy-tabix.

loop at itab where date = itab1-date

and material = itab1-material

and plant = itab1-plant.

lv_sum = lv_sum + itab-quantity.

endloop.

itab1-quantity = lv_sum.

modify itab1 index sy-tabix transporting quantity .

delete itab1 where quantity ne lv_sum

and date = itab1-date

and material = itab1-material

and plant = itab1-plant.

endloop.

Then your itab1 contains the value you need.

Kindly reward points if it helps.