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

Help with Internal Table

former_member207873
Participant
0 Likes
2,501

Hi Experts,

I have an internal table like below.(Fig1) I always need the special GL indicator with 'H' value at the end of same company code and customer number. (Fig2). What logic can be used?

1) Fig:1

Company Code
Customer Number
SPL GL Ind
Amount
1000400000A10000
1000400000  B             20000
1000400000                              H             30000
                          1000400000                              K             10000
                          1700                     400001                             A                  10000
                           1700                     400001                             H              10000
1700                      400001                             K              10000
                           1700400001                            Z               20000

2) Fig:2

Company Code(BUKRS)Customer Number(KUNNR)SPL GL Ind(UMSKZ)Amount(DMBTR)
1000400000A                            10000
1000400000B20000
1000400000K                       100000
                     1000                      400000                        30000
                      1700                        400001                     A                         10000
1700400001K                          10000
1700400001Z20000
1700400001H10000

BR.

1 ACCEPTED SOLUTION
Read only

venkateswaran_k
Active Contributor
0 Likes
2,458

Hi

In your internal table, add another col called sortkey.

While appending the record into this internal table, if the sp_gl_indicator is 'H' then set sortkey = 'Z'.

After completing th internal table,

sort the internal table by companycode customer sortkey and display the report

17 REPLIES 17
Read only

former_member201275
Active Contributor
0 Likes
2,458

If spl_gl = 'H'.

concatenate company_code spl_gl into company_code.

concatenate customer_number spl_gl into customer_number.

endif.

Read only

0 Likes
2,458

I think you didnt get my question.

Read only

0 Likes
2,458

What is your question?  Please clarify.

Regards,

Steve

Read only

0 Likes
2,458

Hi,

loop at table2

read key from table2 in table1

if found, append entry from table1 to table3.

Is this what you are after?

Internal table stuff goes really really quickly in SAP so a simple sequential approach to retrieve the data you need is not a bad way of achieving your goal.

Kind regards, Rob Dielemans

Read only

0 Likes
2,458

Hi

Try like this

TYPES :BEGIN OF ty_final,
        bukrs TYPE bukrs,
        kunnr TYPE kunnr,
        umskz TYPE umskz,
        var   type c,
       END OF ty_final.

DATA :it_final TYPE TABLE OF ty_final,
      wa_final TYPE ty_final.


PERFORM internal_table.


SORT it_final BY bukrs kunnr umskz.

LOOP AT it_final INTO wa_final.

if wa_final-umskz = 'H'.

   wa_final-var = 'X'.

Modify it_final from wa_final TRANSPORTING var.
endif.

endloop.

SORT it_final BY bukrs var.

clear wa_final.

Loop at it_final into wa_final.

write : / wa_final-bukrs,
           wa_final-kunnr,
           wa_final-umskz.
endloop.



*&---------------------------------------------------------------------*
*&      Form  INTERNAL_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM internal_table .
   PERFORM wa_it USING :

   '1000'  '40000' 'A',
   '1000'  '40000' 'H',
   '1000'  '40000' 'B',
   '1000'  '40000' 'K',
   '1700'  '40001' 'A',
   '1700'  '40001' 'K',
   '1700'  '40001' 'H',
   '1700'  '40001' 'Z'.

ENDFORM.                    " INTERNAL_TABLE


*&---------------------------------------------------------------------*
*&      Form  WA_IT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ENDFORM  text
*----------------------------------------------------------------------*
FORM wa_it  USING    v_bukrs TYPE any
                      v_kunnr TYPE any
                      v_umskz TYPE any.

   wa_final-bukrs = v_bukrs.
   wa_final-kunnr = v_kunnr.
   wa_final-umskz = v_umskz.
   APPEND wa_final TO it_final.
   CLEAR wa_final.


ENDFORM.                    " WA_IT


Read only

0 Likes
2,458

Possibly you haven't worded it exactly correctly.

Read only

venkateswaran_k
Active Contributor
0 Likes
2,458

Based on your last update, I am not clear what is your requirement.? Can you please eloborate?

Read only

0 Likes
2,458

Hi All,

Just have a look on the two tables posted in my question. If I have an internal table like figure1 I have to  change it to the way by which it is displayed in figure2. In the first figure you could see the line item with the SPL value (H) is in the third row. But for the same customer number and company code it has to come as fourth row(like in second fig). Similarly for company code 1700 and customer number (400001)..it has to come to the last row from the existing sixth position. is it clear now?

Read only

venkateswaran_k
Active Contributor
0 Likes
2,459

Hi

In your internal table, add another col called sortkey.

While appending the record into this internal table, if the sp_gl_indicator is 'H' then set sortkey = 'Z'.

After completing th internal table,

sort the internal table by companycode customer sortkey and display the report

Read only

0 Likes
2,458

But the spl gl indicators can take up values from A-Z , 1-3. what can be done if there is a SPL GL with 'Z'.

Read only

0 Likes
2,458

You said, you want to push it to the bottom only when it is H.

So, based on the value, you can play wth sortkey.

That will be easier to you

Read only

egenoves
Participant
0 Likes
2,458

Try this code,

first it makes a sort and then it swaps the H row,

please be aware that the code is not very well optimized

data :BEGIN OF wa_data,

         bukrs type bukrs,

         kunnr type kunnr,

         umskz type umskz,

       end of wa_data,

       i_data like standard table of WA_DATA.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'H'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'B'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'Z'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'A'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400001'.

       wa_data-umskz = 'H'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400001'.

       wa_data-umskz = 'B'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400001'.

       wa_data-umskz = 'Z'.

       append wa_data to i_data.

       wa_data-bukrs = '1000'.

       wa_data-kunnr = '400001'.

       wa_data-umskz = 'A'.

       append wa_data to i_data.

       wa_data-bukrs = '1001'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'H'.

       append wa_data to i_data.

       wa_data-bukrs = '1001'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'B'.

       append wa_data to i_data.

       wa_data-bukrs = '1001'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'Z'.

       append wa_data to i_data.

       wa_data-bukrs = '1001'.

       wa_data-kunnr = '400000'.

       wa_data-umskz = 'A'.

       append wa_data to i_data.

       data l_end.

       data:l_index type i,l_tabix type i.

       SORT i_data by bukrs kunnr umskz.

       loop at i_data into wa_data.

         l_tabix = sy-tabix.

         If wa_data-umskz = 'H'.

           l_index = sy-tabix.

           If l_end = 'X'.

             clear l_end.

             continue.

           endif.

         endif.

         at end of kunnr.

           l_end = 'X'.

         endat.

         If l_end = 'X'.

           read table i_data into wa_data index l_index.

           delete i_data index l_index.

*          add 1 to l_tabix.

           insert wa_data into i_data index l_tabix.

         endif.

       endloop.

       loop at i_data into wa_data.

         write:wa_data,/.

       endloop.

Read only

Former Member
0 Likes
2,458

hi,

I am providing you a pseudo code, please built a proper logic on this and it shall solve your problem.

SORT IT_FINAL by BUKRS KUNNR UMSKZ.

IT_TEMP[] = IT_FINAL[].

Loop at IT_TEMP into WA_TEMP where UMSKZ = 'H'.

          Delete IT_FINAL where BUKRS = WA_TEMP-BUKRS KUNNR = WA_TEMP- KUNNR and UMSKZ = WA_TEMP-UMSKZ (basically 'H' record).

          LOOP at IT_FINAL into WA_FINAL where BUKRS = WA_TEMP-BUKRS and KUNNR =                     WA_TEMP- KUNNR.

         

          AT LAST BUKRS.  (use at last and append your H entry as a last record at that index value)

          APPEND using index . ( here at last BUKRS, append the record with 'H' as the last entry using proper index.)

ENDLOOP.

     endif.

ENDLOOP.

In short, check if the table has an 'H' entry, if its there, delete that from current position and put it at last of that bukrs using index.

Please revert if in doubt.

Regards,

DN.

Read only

venkat_aileni
Contributor
0 Likes
2,458

Hi-

Please follow below steps this should work.

Declarations:

1. Declare a new structure with fields Company Code and Customer Number only, say wa_struct.

Steps:

1. Copy all your entries to temp table(it_temp, wa_temp) and use delete all the entries except 'H'.

2. Sort you final internal table with Company Code and Customer Number Ascending/Descending.

3. Loop over your final internal table into workarea wa_final.

     if sy-tabix EQ 1.

          wa_struct = wa_final.

     elseif wa_struct NE wa_final..

          read table it_temp into wa_temp with key bukrs = wa_struct-bukrs

                                                                      kunnr = wa_struct-kunnr.

          if sy-subrc = 0.

               append wa_temp to it_final. "This stmt will append your H value as the last record for                                                                 "C.Code and Customer.

               wa_struct = wa_final.

          endif.

          append wa_final to it_final.

     endif.

   endloop.

Hope this solves your problem.

-Venkat

Read only

former_member196157
Active Participant
0 Likes
2,458

Hiiii....

plaese take the two internal table...........suppose itab1 and itab2

As your first fig.1 is the first internal table....as itab1

Make a another work area  as wa .......

sort itab1 by company code...

Loop at itab1.

itab1-company code.

itab1-customer number.

itab1-spl g/l indicator.

itab1-amount.

on change of company code.

wa-company code.

wa-customer number.

wa-spl g/l indicator. = 'H'.

wa-amount.

append wa to itab2.

endon.

wa = itab1

append itab1 to itab2.

endloop.

do not clear the work area wa...

This may helps u...

Thanks....

Read only

former_member220538
Active Participant
0 Likes
2,458

Hi,

Just swap the rows of the internal based on the indicator value ie use Modify statment to do it.

SORT t_itab BY bukrs kunnr umskz.
LOOP AT t_itab INTO x_itab.
  lp_index = sy-tabix.
  AT END OF bukrs.
    READ TABLE t_itab INTO x_tmp WITH KEY bukrs = x_itab-bukrs
                                          umskz = 'H'.

    IF sy-subrc = 0.
      index = sy-tabix.
    ENDIF.
  ENDAT.
  IF index IS NOT INITIAL.
    MODIFY t_itab FROM x_itab INDEX index.
    MODIFY t_itab FROM x_tmp INDEX lp_index.
    CLEAR index.
  ENDIF.
ENDLOOP.

Regard,

Jeffin

Read only

Former Member
0 Likes
2,458

data: BEGIN OF itab OCCURS 0,

       bukrs type EKKO-bukrs,

      KUNNR type kna1-kunnr,

      shkzg type bseg-shkzg,

     dmbtr type bseg-dmbtr,

   END OF itab.

DATA: F_ITAB LIKE STANDARD TABLE OF ITAB WITH HEADER LINE.

DATA: WA_FTAB LIKE LINE OF ITAB.

   itab-bukrs = '1000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'A'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '1000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'H'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '1000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'K'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '1000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'G'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '7000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'A'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '7000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'K'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '7000'.

   itab-kunnr = '4000'.

   itab-shkzg = 'H'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   itab-bukrs = '7000'.

   itab-kunnr = '40000'.

   itab-shkzg = 'G'.

   ITAB-DMBTR = '20000.00'.

   APPEND ITAB.

   CLEAR: ITAB.

   SORT ITAB BY BUKRS.

   LOOP AT ITAB.

    IF ITAB-SHKZG = 'H'.

      MOVE-CORRESPONDING ITAB TO WA_FTAB.

      ELSE.

        APPEND ITAB TO F_ITAB.

      ENDIF.

      AT END OF BUKRS.

       APPEND WA_FTAB TO F_ITAB.

        ENDAT.

   ENDLOOP.

   LOOP AT  F_ITAB INTO WA_FTAB.

WRITE😕 WA_FTAB-BUKRS, '       ', WA_FTAB-KUNNR, '    ',   WA_FTAB-SHKZG.

   ENDLOOP.

Result:

1000         40000           A

1000         40000           K

1000         40000           G

1000         40000           H

7000         40000           A

7000         40000           K

7000         40000           G

7000         4000            H