6 Replies Latest reply: Aug 8, 2007 2:17 PM by Rodrigo Paisante RSS

column to row display of internal table values

Currently Being Moderated

Hi guys,

I have the internal table in the following manner

 

MATNR     ADSIZE     PRICE

220p     15m     100

220p     16m     120

220p     17m     230

230p     16m     200

230p     17m     120

230p     18m     220

240p     17m     120

250p     18m     560

 

And want to change to the below mentioned manner, please note that there can be multiple matnr's and multiple adsizes as well.

 

MATNR     15m     16m     17m     18m     ......(dynamic:depends on                                                                               

user )

220p     100     120     230

230p     -     200     120     220...and so on.

 

How do i display in the above manner.

Please help.

Thanks in advance.

 

Regards,

Santosh Kotra.

  • Re: column to row display of internal table values
    Sumi Vasu
    Currently Being Moderated

    itab1 = itab.

    Delete adjusacnt duplicates from itab.

     

    loop at itab.

    loop at itab1 where matnr = itab-matnr.

    write ADSIZE .

    endloop.

     

    endloop.

     

    Try this concept.

  • Re: column to row display of internal table values
    Rodrigo Paisante
    Currently Being Moderated

    Hi,

     

    I dont understand your idea, but if you want to change the order, try to create another table and copy respective values to the columns.

     

    Please, explain what layout you want, maybe i can help you to populate the temp IT.

     

    Regards

     

     

    RP

    • Re: column to row display of internal table values
      Currently Being Moderated

      Hi RP,

      I have fetched the data into internal table from a database table and have them in the following fashion.

      MATNR ADSIZE PRICE

      220p     15m       100

      220p     16m       120

      220p     17m       230

      230p     16m       200

      230p      17m      120

      230p      18m      220

      240p      17m     120

      250p      18m     560

       

      And want to change to the below mentioned manner and save into another internal table.

       

      MATNR 15m 16m 17m 18m ......(dynamic:depends on user )

      220p      100 120   230  456

      230p        -    200  120  220...

       

      Please Help.

      Thanks n Regards,

      Santosh Kotra.

  • Re: column to row display of internal table values
    Pawan Kesari
    Currently Being Moderated

    You have to go for dynamic internal table...

     

    refer below program to see how to create dynamic internal table and perform operation on internal table

     

    REPORT zpwtest .
     
    *** Tables
    DATA: lt_data TYPE REF TO data.
    DATA: lt_fieldcatalog TYPE lvc_t_fcat.
     
    *** Structure
    DATA: ls_fieldcatalog TYPE lvc_s_fcat.
     
    *** Data References
    DATA: new_line TYPE REF TO data.
     
    *** Field Symbols
    FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
                   <fs_1> TYPE ANY TABLE,
                   <fs_2>,
                   <fs_3>.
     
     
     
    ls_fieldcatalog-fieldname = 'MANDT'.
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
    ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname
    ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
    ls_fieldcatalog-fieldname = 'CONNID'.
    ls_fieldcatalog-inttype = 'N'.
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
    ls_fieldcatalog-fieldname = 'FLDATE'.
    ls_fieldcatalog-inttype = 'D'.
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
    ls_fieldcatalog-fieldname = 'PRICE'.
    ls_fieldcatalog-inttype = 'P'.
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
    ls_fieldcatalog-fieldname = 'CURRENCY'.
    ls_fieldcatalog-inttype = 'C'.
    APPEND ls_fieldcatalog TO lt_fieldcatalog.
     
     
    ASSIGN lt_data TO <fs_data>.
     
     
    CALL METHOD cl_alv_table_create=create_dynamic_table
         EXPORTING
           it_fieldcatalog = lt_fieldcatalog
         IMPORTING
           ep_table = fs_data
         EXCEPTIONS
           generate_subpool_dir_full = 1
           OTHERS = 2
              .
    IF sy-subrc <> 0.
    ENDIF.
     
     
     
    *** So <FS_1> now points to our dynamic internal table.
     
    ASSIGN <fs_data>->* TO <fs_1>.
     
    *** Next step is to create a work area for our dynamic internal table.
     
    CREATE DATA new_line LIKE LINE OF <fs_1>.
     
    *** A field-symbol to access that work area
    ASSIGN new_line->*  TO <fs_2>.
     
    *** And to put the data in the internal table
    SELECT mandt carrid connid fldate price currency
      FROM sflight
      INTO CORRESPONDING FIELDS OF TABLE <fs_1>.
     
    *** Access contents of internal table
    LOOP AT <fs_1> ASSIGNING <fs_2>.
     
      ASSIGN COMPONENT 1 OF STRUCTURE <fs_2> TO <fs_3>.
      WRITE: / <fs_3>.
    ENDLOOP.
    
    

     

  • Re: column to row display of internal table values
    Rodrigo Paisante
    Currently Being Moderated

    Hi Santosh.

     

    I work hard in this topic. I will post my solution, try to adequate to your needs.

    If your problem is solved, talk to me about your solution.

    I create 2 dynamics tables to do this. I create a it to save the column position to populate the table for alv.

     

    Change the select (form select) to see with others filters.

    I hope this help you and others.

     

    Regards

     

     

     

    &----


    *& Report  ZDYNAMIC                                                    *

    &----


    REPORT  zdynamic                                .

     

        • Tables

    DATA: lt_data TYPE REF TO data,

          lt_new  TYPE REF TO data.

    DATA: lt_fieldcatalog TYPE lvc_t_fcat.

     

        • Structure

    DATA: ls_fieldcatalog TYPE lvc_s_fcat,

          BEGIN OF mat OCCURS 0,

            name TYPE string,

            pos TYPE i,

          END OF mat.

     

        • Data References

    DATA: new_line TYPE REF TO data,

          tab_line TYPE REF TO data.

     

        • Field Symbols

    FIELD-SYMBOLS: <fs_data> TYPE REF TO data,

                   <fs_data1> TYPE REF TO data,

                   <fs_1> TYPE STANDARD TABLE,

                   <ntab> TYPE STANDARD TABLE,

                   <fs_2>,

                   <tab2>,

                   <fs_3>,

                   <tab3>.

     

        • Vars

    DATA: index LIKE sy-index.

     

    PERFORM fieldcat.

     

    ASSIGN lt_data TO <fs_data>.

    ASSIGN lt_new  TO <fs_data1>.

     

    PERFORM cria_tab USING lt_fieldcatalog 1.

     

    ASSIGN <fs_data>->* TO <fs_1>.

    CREATE DATA new_line LIKE LINE OF <fs_1>.

    ASSIGN new_line->*  TO <fs_2>.

     

        • And to put the data in the internal table

    PERFORM select.

     

    • preencher fieldcat tabela nova

    CLEAR lt_fieldcatalog[].

    LOOP AT <fs_1> ASSIGNING <fs_2>.

      DO.

        ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.

        IF sy-subrc <> 0.

          EXIT.

        ENDIF.

        IF sy-index = 1.

          CLEAR ls_fieldcatalog.

          ls_fieldcatalog-coltext   = 'CARRID'.

          ls_fieldcatalog-fieldname = 'CARRID'.

          ls_fieldcatalog-inttype = 'C'.

          APPEND ls_fieldcatalog TO lt_fieldcatalog.

        ENDIF.

        IF sy-index = 2.

          CLEAR ls_fieldcatalog.

          ls_fieldcatalog-coltext   = <fs_3>.

          ls_fieldcatalog-fieldname = <fs_3>.

          ls_fieldcatalog-inttype = 'C'.

          APPEND ls_fieldcatalog TO lt_fieldcatalog.

        ENDIF.

      ENDDO.

    ENDLOOP.

    SORT lt_fieldcatalog DESCENDING BY fieldname.

    DELETE ADJACENT DUPLICATES FROM lt_fieldcatalog.

     

    LOOP AT lt_fieldcatalog INTO ls_fieldcatalog.

      mat-name = ls_fieldcatalog-coltext.

      mat-pos  = sy-tabix.

      APPEND mat.

    ENDLOOP.

     

    *create new table

    PERFORM cria_tab USING lt_fieldcatalog 2.

     

      • add data

    ASSIGN <fs_data1>->* TO <ntab>.

    CREATE DATA tab_line LIKE LINE OF <ntab>.

    ASSIGN tab_line->*  TO <tab2>.

     

    LOOP AT <fs_1> ASSIGNING <fs_2>.

      DO.

        ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.

        IF sy-subrc <> 0.

          EXIT.

        ENDIF.

        IF sy-index = 1.

          ASSIGN COMPONENT sy-index OF STRUCTURE <tab2> TO <tab3>.

          <tab3> = <fs_3>.

        ELSEIF sy-index = 3.

          ASSIGN COMPONENT 2 OF STRUCTURE <fs_2> TO <fs_3>.

          READ TABLE mat WITH KEY name = <fs_3>.

          index = mat-pos.

          ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.

          ASSIGN COMPONENT index OF STRUCTURE <tab2> TO <tab3>.

          <tab3> = <fs_3>.

        ENDIF.

      ENDDO.

      APPEND <tab2> TO <ntab>.

      CLEAR <tab2>.

    ENDLOOP.

    PERFORM display.

     

    ----


    • FORM display

    ----


    FORM display.

      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'

        EXPORTING

          it_fieldcat_lvc = lt_fieldcatalog

        TABLES

    •      t_outtab        = <fs_1>

          t_outtab        = <ntab>

        EXCEPTIONS

          program_error   = 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.

      ENDIF.

    ENDFORM.                    "display

     

    ----


    • FORM fieldcat

    ----


    FORM fieldcat.

     

      CLEAR ls_fieldcatalog.

     

      ls_fieldcatalog-coltext   = 'CARRID'.

      ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname

      ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character

      APPEND ls_fieldcatalog TO lt_fieldcatalog.

     

      ls_fieldcatalog-coltext   = 'CONNID'.

      ls_fieldcatalog-fieldname = 'CONNID'.

      ls_fieldcatalog-inttype = 'N'.

      APPEND ls_fieldcatalog TO lt_fieldcatalog.

     

      ls_fieldcatalog-coltext   = 'FLDATE'.

      ls_fieldcatalog-fieldname = 'FLDATE'.

      ls_fieldcatalog-inttype = 'D'.

      APPEND ls_fieldcatalog TO lt_fieldcatalog.

     

      ls_fieldcatalog-coltext   = 'PRICE'.

      ls_fieldcatalog-fieldname = 'PRICE'.

      ls_fieldcatalog-inttype = 'P'.

      APPEND ls_fieldcatalog TO lt_fieldcatalog.

     

      ls_fieldcatalog-coltext   = 'CURRENCY'.

      ls_fieldcatalog-fieldname = 'CURRENCY'.

      ls_fieldcatalog-inttype = 'C'.

      APPEND ls_fieldcatalog TO lt_fieldcatalog.

     

    ENDFORM.                    "fieldcat

     

    ----


    • FORM cria_tab

    ----


    FORM cria_tab USING catalog TYPE lvc_t_fcat qual TYPE i.

      IF qual = 1.

        CALL METHOD cl_alv_table_create=>create_dynamic_table

          EXPORTING

            it_fieldcatalog           = catalog

          IMPORTING

            ep_table                  = <fs_data>

          EXCEPTIONS

            generate_subpool_dir_full = 1

            OTHERS                    = 2.

      ELSE.

        CALL METHOD cl_alv_table_create=>create_dynamic_table

          EXPORTING

            it_fieldcatalog           = catalog

          IMPORTING

            ep_table                  = <fs_data1>

          EXCEPTIONS

            generate_subpool_dir_full = 1

            OTHERS                    = 2.

      ENDIF.

    ENDFORM.                    "cria_tab

     

    ----


    • FORM select

    ----


    FORM select.

      SELECT mandt carrid connid fldate price currency

        FROM sflight

        INTO CORRESPONDING FIELDS OF TABLE <fs_1>

        WHERE "carrid = 'AA' AND

        fldate > '20080101'.

    ENDFORM.                    "select