‎2008 May 22 2:43 AM
Hi Experts,
I have an internal table with the following fields.
rollno(10) type c,
name(20) type c,
subject(10) type c,
mark(3) type c.
the values stored in the above IT will be something like:
rollno name subject mark
-
100 Sam Maths 80
100 Sam Science 90
100 Sam Social 100
200 Wam Maths 90
200 Wam Science 90
200 Wam Social 80
-
I want the o/p in an internal table like this:
rollno name Tot-mark
-
100 Sam 270
200 Wam 260
-
Please advice as to how i can acheive this
Thanks,
Manju
‎2008 May 22 2:52 AM
Hi,
for the summing of marks you can use COLLECT stmt. But the field you want to add up need not be in char or string. e.g: MARKS type INT. As others are CHAR so they will act like Key for the MARKS field. take ROLLNO or NAME as key.
after addition MOVE source table to Target table(another internal table) for display.
or follow dis below mentioned code
data: begin of itab occurs 10,
roll(10),
name(5),
sub(10),
mark type i,
end of itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math'.
itab-mark = 40.
append itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math1'.
itab-mark = 10.
append itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math2'.
itab-mark = 30.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math'.
itab-mark = 50.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math1'.
itab-mark = 20.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math2'.
itab-mark = 60.
append itab.
itab-roll = '300'.
itab-name = 'sam2'.
itab-sub = 'math'.
itab-mark = 80.
append itab.
itab-roll = '300'.
itab-name = 'sam2'.
itab-sub = 'math1'.
itab-mark = 70.
append itab.
sort itab by roll.
loop at itab.
at new name.
sum.
Write: / itab-roll, itab-name, itab-mark.
endat.
endloop.
Reward if helpful.
Thanks.
Edited by: Sagar@MM on May 22, 2008 10:00 AM
Edited by: Sagar@MM on May 22, 2008 11:33 AM
Edited by: Sagar@MM on May 22, 2008 11:34 AM
‎2008 May 22 2:52 AM
Hi,
for the summing of marks you can use COLLECT stmt. But the field you want to add up need not be in char or string. e.g: MARKS type INT. As others are CHAR so they will act like Key for the MARKS field. take ROLLNO or NAME as key.
after addition MOVE source table to Target table(another internal table) for display.
or follow dis below mentioned code
data: begin of itab occurs 10,
roll(10),
name(5),
sub(10),
mark type i,
end of itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math'.
itab-mark = 40.
append itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math1'.
itab-mark = 10.
append itab.
itab-roll = '100'.
itab-name = 'sam'.
itab-sub = 'math2'.
itab-mark = 30.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math'.
itab-mark = 50.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math1'.
itab-mark = 20.
append itab.
itab-roll = '200'.
itab-name = 'sam1'.
itab-sub = 'math2'.
itab-mark = 60.
append itab.
itab-roll = '300'.
itab-name = 'sam2'.
itab-sub = 'math'.
itab-mark = 80.
append itab.
itab-roll = '300'.
itab-name = 'sam2'.
itab-sub = 'math1'.
itab-mark = 70.
append itab.
sort itab by roll.
loop at itab.
at new name.
sum.
Write: / itab-roll, itab-name, itab-mark.
endat.
endloop.
Reward if helpful.
Thanks.
Edited by: Sagar@MM on May 22, 2008 10:00 AM
Edited by: Sagar@MM on May 22, 2008 11:33 AM
Edited by: Sagar@MM on May 22, 2008 11:34 AM
‎2008 May 22 4:40 AM
hi,
Use COLLECT statement to meet your requirement ..
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/collect.htm
Regards,
Santosh
‎2008 May 22 4:42 AM
Hi,
I have tested in system.' At new' may trigger some unexpect results.
This sample code is correct:
REPORT ZVINCENT_IT_SUM .
TYPES: BEGIN OF TY_SUM,
rollno(10) type c,
name(20) type c,
subject(10) type c,
mark type I,
END OF TY_SUM.
TYPES: BEGIN OF TY_TOT,
rollno(10) type c,
name(20) type c,
mark type I,
END OF TY_TOT.
DATA: IT_SUM TYPE STANDARD TABLE OF TY_SUM,
IT_TOT TYPE STANDARD TABLE OF TY_TOT,
WA_SUM TYPE TY_SUM,
WA_TOT TYPE TY_TOT.
CLEAR WA_SUM.
WA_SUM-rollno = '100'.
WA_SUM-name = 'Sam'.
WA_SUM-subject = 'Maths'.
WA_SUM-mark = '80'.
APPEND WA_SUM TO IT_SUM.
........
........ "data input.
SORT IT_SUM BY ROLLNO NAME.
LOOP AT IT_SUM INTO WA_SUM.
WA_TOT-ROLLNO = WA_SUM-ROLLNO.
WA_TOT-NAME = WA_SUM-NAME.
WA_TOT-MARK = WA_SUM-MARK + WA_TOT-MARK.
AT END OF ROLLNO.
APPEND WA_TOT TO IT_TOT.
CLEAR: WA_TOT.
ENDAT.
ENDLOOP.
LOOP AT IT_TOT INTO WA_TOT.
WRITE:/, WA_TOT-ROLLNO, WA_TOT-NAME, WA_TOT-MARK.
ENDLOOP.
Hope is useful.
Regards
Vincent
‎2008 May 22 4:59 AM
rollno(10) type c,
name(20) type c,
mark(3) type c.
declare new itab as above
loop at itab1 "ur itab.
move corresponding itab to itab1 "new itab
collect itab1.
clear itab1.
endloop.
now itab1 holds the values
‎2008 May 22 5:12 AM
Hi Manju
rollno(10) type c,
name(20) type c,
mark(3) type c.
* Declare new itab as above
loop at itab1 "ur itab.
move corresponding itab to itab1 "new itab
collect itab1.
clear itab1.
endloop.
now itab1 holds the values
Do reward points
Edited by: rajan mehta on May 22, 2008 9:42 AM