cancel
Showing results for 
Search instead for 
Did you mean: 

URGENT ! add selected fields in a table.

Former Member
0 Kudos

Hi,

I have a long table itab like this :

________________________________________

Plant Storage Material Batch Credit Debit ________________________________________

103 1001 125 PDX 00 15

103 1001 125 PEX 00 15

103 1001 125 PEX 00 15

103 1001 125 PEX 25 00

103 1001 226 PDX 00 15

...

When Plant, Storage, Material and Batch are equal I have to make sum of credit and debit and give balance (credit - debit) like this :

Plant Storage Material Batch Credit Debit Bal ____________________________________________

103 1001 125 PDX 00 15 -15

103 1001 125 PEX 25 30 -5

103 1001 226 PDX 00 15 -15

...

It's urgent, can you tell me how to code this? Points awarded!

Tarick.

Accepted Solutions (1)

Accepted Solutions (1)

former_member673464
Active Contributor
0 Kudos

hi,

You can use collect statement for adding the values for it.Collect statement will addup the numeric fields when the others fields are same.Otherwise it will append the entry to internal table.

Regards,

Veeresh

Answers (8)

Answers (8)

Former Member
0 Kudos

Hi,

try this short example:

TYPES: BEGIN OF TY_ITAB,

WERKS LIKE MARD-WERKS,

LGORT LIKE MARD-LGORT,

MATNR LIKE MARD-MATNR,

BATCH(3),

CREDI TYPE I,

DEBIT TYPE I,

BALAN TYPE I,

END OF TY_ITAB.

*

DATA IT_ITAB TYPE TABLE OF TY_ITAB.

DATA WA_ITAB TYPE TY_ITAB.

*

WA_ITAB-WERKS = '103'.

WA_ITAB-LGORT = '1001'.

WA_ITAB-MATNR = '125'.

WA_ITAB-BATCH = 'PDX'.

WA_ITAB-CREDI = 0.

WA_ITAB-DEBIT = 15.

COLLECT WA_ITAB INTO IT_ITAB.

*

WA_ITAB-WERKS = '103'.

WA_ITAB-LGORT = '1001'.

WA_ITAB-MATNR = '125'.

WA_ITAB-BATCH = 'PEX'.

WA_ITAB-CREDI = 0.

WA_ITAB-DEBIT = 15.

COLLECT WA_ITAB INTO IT_ITAB.

*

WA_ITAB-WERKS = '103'.

WA_ITAB-LGORT = '1001'.

WA_ITAB-MATNR = '125'.

WA_ITAB-BATCH = 'PEX'.

WA_ITAB-CREDI = 0.

WA_ITAB-DEBIT = 15.

COLLECT WA_ITAB INTO IT_ITAB.

*

WA_ITAB-WERKS = '103'.

WA_ITAB-LGORT = '1001'.

WA_ITAB-MATNR = '125'.

WA_ITAB-BATCH = 'PEX'.

WA_ITAB-CREDI = 25.

WA_ITAB-DEBIT = 0.

COLLECT WA_ITAB INTO IT_ITAB.

*

WA_ITAB-WERKS = '103'.

WA_ITAB-LGORT = '1001'.

WA_ITAB-MATNR = '226'.

WA_ITAB-BATCH = 'PDX'.

WA_ITAB-CREDI = 0.

WA_ITAB-DEBIT = 15.

COLLECT WA_ITAB INTO IT_ITAB.

*

LOOP AT IT_ITAB INTO WA_ITAB.

WRITE: / WA_ITAB-CREDI, WA_ITAB-DEBIT, WA_ITAB-BALAN.

ENDLOOP.

LOOP AT IT_ITAB INTO WA_ITAB.

WA_ITAB-BALAN = WA_ITAB-CREDI - WA_ITAB-DEBIT.

MODIFY IT_ITAB FROM WA_ITAB.

ENDLOOP.

LOOP AT IT_ITAB INTO WA_ITAB.

WRITE: / WA_ITAB-CREDI, WA_ITAB-DEBIT, WA_ITAB-BALAN.

ENDLOOP.

Regards, Dieter

Former Member
0 Kudos

Hi,

Just do a simple calculation and den modify the internal table for the balance's field. You may refer to a simple program which i wrote that serves your purpose. The trick i did was to add another field to your current internal table which is balance and then use the MODIFY syntax to modify your internal table after you done with the calculation. Do reward if you find it useful.

Thanks,

Loo



report zastest.

*Structures for the header
Types : Begin of struc_header,
          plant(10)      type c,
          storage(10)    type c,
          material(10)   type c,
          batch(10)      type c,
          credit(5)      type c,
          debit(5)       type c,
          bal(5)         type c,
        end of struc_header.

Data : h_itab type table of struc_header,
       wa_htab like line of h_itab.

*Structure for the internal table that holds your data
Types : Begin of struc_tab,
          plant(3)      type c,
          storage(4)    type c,
          material(3)   type c,
          batch(3)      type c,
          credit(5)    type c,
          debit(5)      type c,
          bal(5)        type c,
        end of struc_tab.

Data : itab type table of struc_tab,
       wa_itab like line of itab.

Data : l_line type i.

Start-of-selection.

move : 'Plant'    to wa_htab-plant,
       'Storage'  to wa_htab-storage,
       'Material' to wa_htab-material,
       'Batch'    to wa_htab-batch,
       'Credit'   to wa_htab-credit,
       'Debit'    to wa_htab-debit,
       'Bal'      to wa_htab-bal.

append wa_htab to h_itab. Clear wa_htab.


move : '103'   to wa_itab-plant,
       '1001'  to wa_itab-storage,
       '125'   to wa_itab-material,
       'PDX'   to wa_itab-batch,
       '00'    to wa_itab-credit,
       '15'    to wa_itab-debit.

append wa_itab to itab. Clear wa_itab.

move : '103'   to wa_itab-plant,
       '1001'  to wa_itab-storage,
       '125'   to wa_itab-material,
       'PDX'   to wa_itab-batch,
       '00'    to wa_itab-credit,
       '15'    to wa_itab-debit.

append wa_itab to itab. Clear wa_itab.

*Check if internal table has value
Describe table itab lines l_line.

If l_line GT 0.

*write the header 
loop at h_itab into wa_htab.
 write :  wa_htab-plant,
          wa_htab-storage,
          wa_htab-material,
          wa_htab-batch,
          wa_htab-credit,
          wa_htab-debit,
          wa_htab-bal.
endloop.

  loop at itab into wa_itab.
*Perform your calculation and modify your internal table
    wa_itab-bal = wa_itab-credit - wa_itab-debit.
    MODIFY itab INDEX sy-tabix FROM wa_itab
             TRANSPORTING bal.

*write the data 
    write : / wa_itab-plant    under wa_htab-plant,
              wa_itab-storage  under wa_htab-storage,
              wa_itab-material under wa_htab-material,
              wa_itab-batch    under wa_htab-batch,
              wa_itab-credit   under wa_htab-credit,
              wa_itab-debit    under wa_htab-debit,
              wa_itab-bal      under wa_htab-bal.
  endloop.
endif.


Edited by: ~loObie on Jun 9, 2008 11:37 AM

Edited by: ~loObie on Jun 9, 2008 11:39 AM

Former Member
0 Kudos

hi Prem,

here's what you wrote :

loop at itab into wa1.

loop at itab into wa2.

loop at itab into wa3.

loop at itab into wa4.

if wa4-batch eq wa3-batch and wa4-batch eq wa2-batch and wa4-batch eq wa1-batch.

select credit debit into var1 var2 from itab where wa4-batch eq batch.

sum1 = var1+var2.

write : /wa4-plant , wa4-storage, wa4-material, wa4-batch, wa4-credit, wa4-debit, sum1.

endif.

endloop.

endloop.

endloop.

endloop.

Can you tell me how should I declare wa1, wa2, wa3, wa4?

Are they the same work area structure as itab but with different name?

Former Member
0 Kudos

Hi,

I need to add lines by credit and debit ONLY when the 4 fields Plant, Storage, Material and Batch are equal.

And the table is very long...

Any help?

Tarick

Former Member
0 Kudos

try the above example that i have given.... it wil definitly help u out.

Former Member
0 Kudos

hi there.....

do as follows::

loop at itab into wa1.

loop at itab into wa2.

loop at itab into wa3.

loop at itab into wa4.

if wa4-batch eq wa3-batch and wa4-batch eq wa2-batch and wa4-batch eq wa1-batch.

select credit debit into var1 var2 from itab where wa4-batch eq batch.

sum1 = var1+var2.

write : /wa4-plant , wa4-storage, wa4-material, wa4-batch, wa4-credit, wa4-debit, sum1.

endif.

endloop.

endloop.

endloop.

endloop.

i hope it helps.

do reward if helpful.

Former Member
0 Kudos

Do like this.

loop at i_tab.

at end of credit.

sum.

write:/ i_tab-credit.

endat.

at end of debit.

sum.

write:/ i_tab-debit.

endat.

endloop.

Regards,

Jagadish

Former Member
0 Kudos

Use internal table Control event AT END OF batch in the LOOP and use keyword SUM and write the logic for balance CREDIT-DEBIT and then append it to a new internal table.

Regards,

Kiran Bobbala

Former Member
0 Kudos

Hi ,

use control break statements like AT END OF ...

loop at itab.

write ...

at end of credit.

sum.

write:/ itab-credit.

endat.

at end of debit.

sum.

write:/ itab-debit.

endat.

endloop.

hope this will help u to make sum of credit and debit.

Regards,

Vishvesh

if helpful, rewards it.