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: 

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

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

5 REPLIES 5

former_member202818
Active Contributor
0 Kudos

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

0 Kudos

Hi Sreekanth ,

Go through the link :

http://scn.sap.com/docs/DOC-42525

This will help you out

Regards

Yogendra Bhaskar

Former Member
0 Kudos

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

SharathSYM
Contributor
0 Kudos

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

Former Member
0 Kudos

I got my answer .

Thanks to all for giving help to me.