Skip to Content
author's profile photo
Former Member

Internal table query

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

9 Answers

  • Best Answer
    author's profile photo
    Former Member
    Sep 30, 2005 at 09:12 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:03 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 30, 2005 at 09:05 AM

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

    Christian

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:06 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:10 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:13 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:15 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Sep 30, 2005 at 09:17 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 30, 2005 at 09:38 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded