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

Need Logic in Read statement

Former Member
0 Likes
1,585

Hi guys,

I have an intenal table for suppose say 5 records with duplicate entrys.

My ITAB looks some thing like

TKNUM weight volume

1 10 5

1 8 6

1 15 10

2 30 15

2 20 9

I need to calculate total weight and volume for each TKNUM (Shipment number) i.e,

After calculation my new internal table should look like

TKNUM Total Weight Total Volume

1 33 21

2 50 24

How to do this.

Thanks in Advance.

Prasad.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,544

sort itab by tknum.

loop at itab

into wa_itab.

at end of tknum.

sum.

append wa_itab to itab1.

endat.

endloop.

Regards

Varun

16 REPLIES 16
Read only

Former Member
0 Likes
1,544

Hi:

under the loop

total for volume = + volume.

total for weight = + weight.

Regards

Shashi

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,544

Hi,

Here is the solution.

data : begin of ty occurs 0,

f1 type i,

f2 type i,

f3 type i,

end of ty.

ty-f1 = 1.

ty-f2 = 10.

ty-f3 = 5.

append ty.

ty-f1 = 1.

ty-f2 = 8.

ty-f3 = 6.

append ty.

ty-f1 = 1.

ty-f2 = 15.

ty-f3 = 10.

append ty.

ty-f1 = 2.

ty-f2 = 30.

ty-f3 = 15.

append ty.

ty-f1 = 2.

ty-f2 = 20.

ty-f3 = 9.

append ty.

loop at ty.

at end of f1.

sum.

write : / ty-f1, ty-f2, ty-f3.

endat.

endloop.

Read only

0 Likes
1,544

Hi jayanti,

I mean to say that i dont have same data in internal table it keeps on changing.

I need to calculate the total weight and volume for that shipment.

Thanks,

Prasad.

Read only

0 Likes
1,544

Hi,

As long as you have a key field, this solution will work.I just had taken an example.

For the key field, use

loop ...

at end of TKNUM.

sum.

write :....

endat.

endloop.

Read only

awin_prabhu
Active Contributor
0 Likes
1,544

Hi friend,

Use COLLECT statements.

Collect itab.

See F1 help on COLLECT

Thanks..

Edited by: Sap Fan on Feb 23, 2009 11:10 AM

Read only

Former Member
0 Likes
1,544

Hi Prasad,

Use the following logic.

I assume your internal table name is IT_OLD which has the follwing data.

TKNUM weight volume

1 10 5

1 8 6

1 15 10

2 30 15

2 20 9

No do the following:

data: it_new like it_old occurs 0 with header line.

SORT IT_OLD by TKNUM.

LOOP AT IT_OLD.

AT NEW TKNUM.

SUM.

MOVE-CORRESPONDING IT_OLD TO IT_NEW.

APPEND IT_NEW.

ENDAT.

ENDLOOP.

Check the contents of IT_NEW after the loop for the results.

Regards,

Prakash Pandey

Read only

0 Likes
1,544

Hi Prakash,

Thanks for your reply.

One more small information is I have 1 other fileds with other data.

Means

TKNUM weight volume x

1 10 5 abc

1 8 6 def

1 15 10 ac

2 30 15 bc

2 20 9 zz

I dont need this x data in the newly calculated internal table.

Then how to eliminate this...

Thanks,

Prasad.

Read only

0 Likes
1,544

Dont move it ... thats it...

Try using powerful statements ex: 'collect' for this scenario.

Read only

Former Member
0 Likes
1,545

sort itab by tknum.

loop at itab

into wa_itab.

at end of tknum.

sum.

append wa_itab to itab1.

endat.

endloop.

Regards

Varun

Read only

faisalatsap
Active Contributor
0 Likes
1,544

Hi,

Please Check your Subject i think it is not Relevant to your Question

If your First Field is type c than you can use COLLECT for Sample Code Have a look at my Post in the following Thread,

[Collect Sample Code|;

Please Reply if any Issue,

Kind Regards,

Faisal

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,544

if tknum is of datatype char.

then

declare itab2 same as itab1.

loop at itab1 into wa.

collect wa into itab2.

endloop.

if not char type go for control break statements

Read only

Former Member
0 Likes
1,544

Hi Prasad,

use collect statement to append the records..

declare an internal table with the duplicate entries col as a character field..


types: begin of type_s_tab,
       col1 type c,
       col2 type i,
       col3 type i,
       end of type_s_tab.
       data fs_tab type type_s_tab.
       data: itab like table of fs_tab with header line.

       itab-col1 = 1.
       itab-col2 = 10.
       itab-col3 = 5.
       append itab.

       itab-col1 = 1.
       itab-col2 = 8.
       itab-col3 = 6.
       collect itab.
       
       
       loop at itab.
       write:/ itab-col1,
               itab-col2,
               itab-col3.
               endloop.

Regards,

Mdi.Deeba

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,544

Hi,

loop at itab.

at end of tknum.

sum.

write : / itab-tknum, itab-weight, itab-volume.

endat.

endloop.

Read only

Former Member
0 Likes
1,544

Use COLLECT stmt.

Data :

it_tab type standard table of t_your_structure, //your internal table having 5 records.

it_tab1 type standard table of t_your_structure, // internal table after calculation.

wa_tab type t_your_structure.

Loop at it_tab into wa_tab.

collect wa_tab into it_tab1.

endloop.

Read only

Former Member
0 Likes
1,544

Hi guys,

I have an intenal table for suppose say 5 records with duplicate entrys.

My ITAB looks some thing like

TKNUM weight volume

1 10 5

1 8 6

1 15 10

2 30 15

2 20 9

I need to calculate total weight and volume for each TKNUM (Shipment number) i.e,

After calculation my new internal table should look like

TKNUM Total Weight Total Volume

1 33 21

2 50 24

How to do this.

Thanks in Advance.

Prasad.

Hi Prasad,

You can use following code.

SORT itab BY tknum weight volume.

DATA: lv_itab LIKE itab ,

lv_weight LIKE itab-weight,

lv_volume LIKE itab-volume.

LOOP AT itab.

move itab to lv_itab.

lv_weight = lv_weight + itab-weight.

lv_volume = lv_volume + itab-volume.

AT END OF

itab1-tknum = lv_itab-tknum.

itab1-weight = lv_weight.

itab1-volume = lv_volume.

append itab1.

clear: itab1 ,

lv_weight,

lv_volume.

ENDAT.

ENDLOOP.

Hope this help you.

Regards,

Anil

Read only

Former Member
0 Likes
1,544

Thanks to one and all......