Skip to Content
author's profile photo Former Member
Former Member

How we run loop for clubbing rows and then convert into column

My requirement is:

in my internal table ittab1 for single date there are more than one entries corresponding to different agency no.

Date code Value

18.03.2012 02 2.22

18.03.2012 03 0.22

17.02.2012 02 0.12

17.02.2012 04 1.19

17.02.2012 03 2.21

.

.

.

I want to create another internal table in which I want for one date there is only a single row by converting row entries into columns.

pls suggest how I run loop to achieve the below internal table.

Internal Table name : ittab2

Date 01 02 03 04 05 .......

18.03.2012 2.22 0.22

17.03.2012 0.12 2.21 1.19

Thanks,

Samiksha.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Jun 11, 2014 at 07:35 AM

    Hi Samiksha,

    if your requirement is having fixed CODE values like only code values 02,03 to 05 then we can create a separate internal table in our program statically as below.

    types : begin of ty_itab1,

    date type datum,

    01 type <value>,

    02 type <value>,

    ....

    05 type <value>,

    end of ty_itab1.

    data: target_itab type standard table of ty_itab,

    target_wa type ty_itab,

    lv_date type datum.

    sort ittab1 by date.

    read table ittab1 into wa_ittab1 index1.

    if sy-subrc = 0.

    lv_date = wa_ittab1-date.

    endif.

    loop at ittab1 into wa_ittab1.

    if lv_date = wa_ittab1-date.

    target_wa-date = wa_ittab1-date.

    case wa_ittab1-code

    when '01'

    target_wa-01 = wa_ittab1-value.

    when '02'

    target_wa-02 = wa_ittab1-value.

    ......

    when '05'

    target_wa-05 = wa_ittab1-value.

    endcase.

    else.

    append target_wa to target_itab.

    lv_date = wa_ittab1-date.

    target_wa-date = wa_ittab1-date.

    case wa_ittab1-code

    when '01'

    target_wa-01 = wa_ittab1-value.

    when '02'

    target_wa-02 = wa_ittab1-value.

    ......

    when '05'

    target_wa-05 = wa_ittab1-value.

    endcase.

    endif.

    endloop.

    now all the values corresponding to the codes will get stored in the target table TARGET_ITABfor a single date.

    but this apply only if we have limited no.of columns as CODEs.

    let us know incase of any issues further.

    thanks,

    Bhaskar

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 11, 2014 at 07:33 AM

    Hi,

    You have to have a dynamic internal table.

    Because your internal table column may change depend on the different dates.

    say..

    Date code1 value1 code2 value2 ...etc.

    So u sort your initial internal table by date.

    Now found the maximum number of columns(code & value) required.

    Date code Value

    18.03.2012 02 2.22

    18.03.2012 03 0.22

    17.02.2012 02 0.12

    17.02.2012 04 1.19

    17.02.2012 03 2.21

    here 17.02.2012 has 3 records, then there will be columns up to code3 value3.

    to create a internal table of this type dynamically, use the below code.

    DATA: git_fldcat_2 TYPE lvc_t_fcat. "For creating dynmic table

    DATA : go_newtable TYPE REF TO data, "To Create Dynamic Table

    go_newtable_wa TYPE REF TO data, "To Create Dynamic Work Area

    go_newtable_wa_1 TYPE REF TO data. "To Create Dynamic Work Area

    FIELD-SYMBOLS : <fs_it> TYPE STANDARD TABLE, "Dynamic intenal table

    <fs_wa>. "Dynamic work area


    Fill git_fieldcat_2 with fields...

    say Date code1 value1 code2 value2 ...etc.



    *Create Dynamic Table

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    it_fieldcatalog = git_fldcat_2 "Field Catlog of Type LVC

    IMPORTING

    ep_table = go_newtable

    EXCEPTIONS

    generate_subpool_dir_full = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    RETURN.

    ENDIF.

    ASSIGN go_newtable->* TO <fs_it>. "Dynamic Internal Table

    CREATE DATA go_newtable_wa LIKE LINE OF <fs_it>.

    ASSIGN go_newtable_wa->* TO <fs_wa>. "Dynamic Work Area

    CREATE DATA go_newtable_wa_1 LIKE LINE OF <fs_it>.

    ASSIGN go_newtable_wa_1->* TO <fs_wa_1>. "Dynamic Work Area

    Regards

    Sreekanth

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 11, 2014 at 07:40 AM

    Hi Samiksha,

    You can use control break statements,

    Code as follows: (not tested)

    data l_count type i.

    FIELD-SYMBOLS: <F1>, <F3>.

    l_count = 1.

    Sort itab1 by date.

    Loop at itab1 into wa_itab1.

    l_count = l_count + 1.

    ASSIGN wa_itab2 TO <F1>.

    ASSIGN COMPONENT l_count OF STRUCTURE <F1> TO <F3>.

    <F3> = wa_itab1-value.

    at end of date.

    append wa_itab2 TO itab2.

    l_count = 1.

    endat.

    endloop.

    Thanks,

    Sharath

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 11, 2014 at 09:15 AM

    I got my answer .

    Thanks to all for giving help to me.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.