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

issue

Former Member
0 Likes
2,108

Guy's

i have got one internal table with following values

FIELD-> KSTAR WRTTP RKG001 RKG002 RKG003

VALUE-> 70200100 4 11245.36 12071.93 0

VALUE-> 73011100 4 6866.41 310.05 0

VALUE-> 73011100 4 12 0 1710

i want duplicate record in this internal table to

be summed up as below for duplicate entries

VALUE-> 70200100 4 11245.36 12071.93 0

VALUE-> 73011100 4 6878.41 310.05 1710

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,067

Hi Sanju,

Some minor changes in your code and you are good to go.

  • Code starts here

data: begin of t_itab occurs 0 ,

kstar like cosp-kstar,

objnr like cosp-objnr,

wrttp like cosp-wrttp,

wkg001 like cosp-wkg001,

wkg002 like cosp-wkg001,

wkg003 like cosp-wkg001.

data: end of t_itab.

data: t_itab1 like standard table of t_itab with header line.

data: wa_itab1 like t_itab,

wa_itab2 like t_itab.

data: v_rkg001 type cosp-wkg001 ,

v_rkg002 type cosp-wkg001 ,

v_rkg003 type cosp-wkg001 .

*FIELD-> KSTAR WRTTP RKG001 RKG002 RKG003

*VALUE-> 70200100 4 11245.36 12071.93 0

*VALUE-> 73011100 4 6866.41 310.05 0

*VALUE-> 73011100 4 12 0 1710

*

break-point.

t_itab-kstar = '70200100'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200101'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200100'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

sort t_itab by kstar.

loop at t_itab into wa_itab1.

at new kstar.

clear : v_rkg001, v_rkg002, v_rkg003.

endat.

move wa_itab1 to wa_itab2.

v_rkg001 = v_rkg001 + t_itab-wkg001.

v_rkg002 = v_rkg002 + t_itab-wkg002.

v_rkg003 = v_rkg003 + t_itab-wkg003.

at end of kstar.

t_itab1-objnr = wa_itab2-objnr.

t_itab1-wrttp = wa_itab2-wrttp.

t_itab1-kstar = wa_itab2-kstar.

t_itab1-wrttp = wa_itab2-wrttp.

t_itab1-wkg001 = v_rkg001.

t_itab1-wkg002 = v_rkg002.

t_itab1-wkg003 = v_rkg003.

append t_itab1.

endat.

endloop.

refresh t_itab.

t_itab[] = t_itab1[].

  • Code ends here

Cheers,

Vikram

Pls reward for helpful replies!!

15 REPLIES 15
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,067

You will need to loop at that internal table and then use the COLLECT statement to collect the data into a second internal table.

Loop at itab1.

move itab1 to itab2.
collect itab2.

endloop.

With the structure that you have there, all you need is another internal table just like the first, then COLLECT them into the second.

Regards,

Rich Heilman

Read only

0 Likes
2,067

Also, if you have control over how the first ITAB is filled, you could COLLECT there instead.

Regards,

Rich Heilman

Read only

0 Likes
2,067

That is true. I was assuming that WRTTP was a character field. If you change the definition to a 2 character field instead of a 2 numeric in your internal table, then COLLECT will work.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,067

Hi,

You will have to loop at the table and add it yourself. I don't there is any automatic way of doing this. COLLECT won't work as you don't want some of the numeric fields to be added.

Regards,

Ravi

Read only

Former Member
0 Likes
2,067

use <b>COLLECT</b> statement for addition. But your code seems to be that it has to delete duplicate entries and then display for that use <b>DELETE ADJACENT DUPLICATES</b> after using SORT statement.

Read only

Former Member
0 Likes
2,067

Hi Sanju,

You can also use the <b>AT NEW</b> command after looping the internal table.

Regards,

Arun Sambargi.

Read only

Former Member
0 Likes
2,067

Hi,.

For Collect to Work ,

WRTTP should be char..if not it shud be converted to char and then COLLECT can be used

OR ELSE

we would have to loop and add manually as Ravi said..

Regards,

Tanveer.

Read only

Former Member
0 Likes
2,067

Hi,

sort itab by KSTAR.

loop at itab.

at end of kstar.

sum.

endat.

endloop.

Regards

Amole

Read only

Former Member
0 Likes
2,067

Hi,

you can also do this way..

it_backup[] = itab[].

loop at it_backup.

clear it_backup-wrttp.

endloop.

loop at it_backup.

move it_backup to itab_final.

collect itab_final.

endloop.

later you can modify the itab_final-wrttp from original itab.

and what if..

VALUE-> 73011100 <b>4</b> 6866.41 310.05 0

VALUE-> 73011100 <b>5</b> 12 0 1710

then also you want the same results....

or different..

Regards

vijay

Read only

Former Member
0 Likes
2,067

Hi Sanju,

you can do it by many ways the easiest and most efficint method is use <b>COLLECT</b> statement as shown below,

this is an example : all the numeric field get added u will have to take care while there are non-numeric fields.

TYPES: BEGIN OF COMPANY,

NAME(20) TYPE C,

SALES TYPE I,

END OF COMPANY.

DATA: COMP TYPE COMPANY,

COMPTAB TYPE HASHED TABLE OF COMPANY

WITH UNIQUE KEY NAME.

COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB.

Table COMPTAB now has the following contents:

NAME | SALES

-


Duck | 40

Tiger | 20

one more way to do it is use AT NEW or ON CHANGE OF COMMAND inside table loop.

Read only

Former Member
0 Likes
2,067

Hi Sanju,

Declare an internal table itab2 like your original internal table itab1. Alos delcare work areas and varaibles v_rkg001, v_rkg002, v_rkg003.

*Code starts here

sort itab1 by KSTAR.

loop at itab1 into wa_itab1.

at new KSTAR.

clear : v_rkg001, v_rkg002, v_rkg003.

endat.

move wa_itab1 to wa_itab2.

v_rkg001 = v_rkg001 + itab1-rkg001.

v_rkg002 = v_rkg002 + itab1-rkg002.

v_rkg003 = v_rkg003 + itab1-rkg003.

at end of KSTAR.

itab2-KSTAR = wa_itab2-KSTAR.

itab2-WRTTP = wa_itab2-WRTTP.

itab2-RKG001 = v_rkg001.

itab2-RKG002 = v_rkg002.

itab2-RKG003 = v_rkg003.

append itab2.

endat.

endloop.

*Code ends here

Now your itab2 contains the required data. You can either use this internal table or just refresh original table and move itab2 to itab1 using :

refresh itab1.

itab1[] = itab2[].

Cheers,

Vikram

Pls reward for helpful replies!!

Read only

0 Likes
2,067

Hi vikram

your code help me lot to implement logic in my program

below is the test program that i created,it's working

fine ,pls let me know if you fell that i should add some

other stuff ,before implementing this code in my actual

program.

REPORT ztest_1 MESSAGE-ID 00 .

DATA: BEGIN OF T_ITAB OCCURS 0 ,

OBJNR LIKE COSP-OBJNR,

KSTAR LIKE COSP-KSTAR,

WRTTP LIKE COSP-WRTTP,

WKG001 LIKE COSP-WKG001,

WKG002 LIKE COSP-WKG001,

WKG003 LIKE COSP-WKG001.

DATA: END OF T_ITAB.

DATA: BEGIN OF T_ITAB1 OCCURS 0 ,

OBJNR LIKE COSP-OBJNR,

KSTAR LIKE COSP-KSTAR,

WRTTP LIKE COSP-WRTTP,

WKG001 LIKE COSP-WKG001,

WKG002 LIKE COSP-WKG001,

WKG003 LIKE COSP-WKG001.

DATA: END OF T_ITAB1.

DATA: BEGIN OF wa_itab1 ,

OBJNR LIKE COSP-OBJNR,

KSTAR LIKE COSP-KSTAR,

WRTTP LIKE COSP-WRTTP,

WKG001 LIKE COSP-WKG001,

WKG002 LIKE COSP-WKG001,

WKG003 LIKE COSP-WKG001.

DATA: END OF wa_itab1.

DATA: BEGIN OF wa_itab2 ,

OBJNR LIKE COSP-OBJNR,

KSTAR LIKE COSP-KSTAR,

WRTTP LIKE COSP-WRTTP,

WKG001 LIKE COSP-WKG001,

WKG002 LIKE COSP-WKG001,

WKG003 LIKE COSP-WKG001.

DATA: END OF wa_itab2.

*FIELD-> KSTAR WRTTP RKG001 RKG002 RKG003

*VALUE-> 70200100 4 11245.36 12071.93 0

*VALUE-> 73011100 4 6866.41 310.05 0

*VALUE-> 73011100 4 12 0 1710

*

T_ITAB-KSTAR = '70200100'.

T_ITAB-OBJNR = '70200100'.

T_ITAB-WRTTP = '4'.

T_ITAB-WKG001 = '100'.

T_ITAB-WKG002 = '200'.

T_ITAB-WKG003 = '300'.

APPEND T_ITAB.

T_ITAB-KSTAR = '70200101'.

T_ITAB-OBJNR = '70200100'.

T_ITAB-WRTTP = '4'.

T_ITAB-WKG001 = '100'.

T_ITAB-WKG002 = '200'.

T_ITAB-WKG003 = '300'.

APPEND T_ITAB.

T_ITAB-KSTAR = '70200100'.

T_ITAB-OBJNR = '70200100'.

T_ITAB-WRTTP = '4'.

T_ITAB-WKG001 = '100'.

T_ITAB-WKG002 = '200'.

T_ITAB-WKG003 = '300'.

APPEND T_ITAB.

sort T_itab by KSTAR.

loop at T_itab into wa_itab1.

at new KSTAR.

DATA: v_rkg001 TYPE COSP-WKG001 ,

v_rkg002 TYPE COSP-WKG001 ,

v_rkg003 TYPE COSP-WKG001 .

clear : v_rkg001, v_rkg002, v_rkg003.

endat.

move wa_itab1 to wa_itab2.

v_rkg001 = v_rkg001 + T_itab-Wkg001.

v_rkg002 = v_rkg002 + T_itab-Wkg002.

v_rkg003 = v_rkg003 + T_itab-Wkg003.

at end of KSTAR.

T_itab1-OBJNR = wa_itab2-OBJNR.

T_itab1-WRTTP = wa_itab2-WRTTP.

T_itab1-KSTAR = wa_itab2-KSTAR.

T_itab1-WRTTP = wa_itab2-WRTTP.

T_itab1-WKG001 = v_rkg001.

T_itab1-WKG002 = v_rkg002.

T_itab1-WKG003 = v_rkg003.

append T_itab1.

endat.

endloop.

refresh T_itab.

T_itab[] = T_itab1[].

Read only

0 Likes
2,067

Your really making it hard on yourself. You can just do this.



report zrich_0001 .

data: begin of t_itab occurs 0 ,
objnr like cosp-objnr,
kstar like cosp-kstar,
wrttp like cosp-wrttp,
wkg001 like cosp-wkg001,
wkg002 like cosp-wkg001,
wkg003 like cosp-wkg001.
data: end of t_itab.

data: begin of t_itab1 occurs 0 ,
objnr like cosp-objnr,
kstar like cosp-kstar,
<b>wrttp(2) type c,  "" like cosp-wrttp,</b>
wkg001 like cosp-wkg001,
wkg002 like cosp-wkg001,
wkg003 like cosp-wkg001.
data: end of t_itab1.

data: wa_itab1 like line of t_itab1.


*FIELD-> KSTAR WRTTP RKG001 RKG002 RKG003
*VALUE-> 70200100 4 11245.36 12071.93 0
*VALUE-> 73011100 4 6866.41 310.05 0
*VALUE-> 73011100 4 12 0 1710
*


t_itab-kstar = '70200100'.
t_itab-objnr = '70200100'.
t_itab-wrttp = '4'.
t_itab-wkg001 = '100'.
t_itab-wkg002 = '200'.
t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200101'.
t_itab-objnr = '70200100'.
t_itab-wrttp = '4'.
t_itab-wkg001 = '100'.
t_itab-wkg002 = '200'.
t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200100'.
t_itab-objnr = '70200100'.
t_itab-wrttp = '4'.
t_itab-wkg001 = '100'.
t_itab-wkg002 = '200'.
t_itab-wkg003 = '300'.

append t_itab.

sort t_itab by kstar.
<b>loop at t_itab into wa_itab1.
collect wa_itab1 into t_itab1.
endloop.</b>

refresh t_itab.
t_itab[] = t_itab1[].

Regards,

Rich Heilman

Read only

0 Likes
2,067

ThANKS RICH .

point here is i am working on z report created

by someone else ,this report is clone of standard report,

and i didn't mention that there are so many field's

beside the one which i have mentioned u,so changing

the field can cause some impact,instead we can concentrate

on field which hold currency value as vikram suggested me

,any how both of u deserve point for analyzing this issue

Read only

Former Member
0 Likes
2,068

Hi Sanju,

Some minor changes in your code and you are good to go.

  • Code starts here

data: begin of t_itab occurs 0 ,

kstar like cosp-kstar,

objnr like cosp-objnr,

wrttp like cosp-wrttp,

wkg001 like cosp-wkg001,

wkg002 like cosp-wkg001,

wkg003 like cosp-wkg001.

data: end of t_itab.

data: t_itab1 like standard table of t_itab with header line.

data: wa_itab1 like t_itab,

wa_itab2 like t_itab.

data: v_rkg001 type cosp-wkg001 ,

v_rkg002 type cosp-wkg001 ,

v_rkg003 type cosp-wkg001 .

*FIELD-> KSTAR WRTTP RKG001 RKG002 RKG003

*VALUE-> 70200100 4 11245.36 12071.93 0

*VALUE-> 73011100 4 6866.41 310.05 0

*VALUE-> 73011100 4 12 0 1710

*

break-point.

t_itab-kstar = '70200100'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200101'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

t_itab-kstar = '70200100'.

t_itab-objnr = '70200100'.

t_itab-wrttp = '4'.

t_itab-wkg001 = '100'.

t_itab-wkg002 = '200'.

t_itab-wkg003 = '300'.

append t_itab.

sort t_itab by kstar.

loop at t_itab into wa_itab1.

at new kstar.

clear : v_rkg001, v_rkg002, v_rkg003.

endat.

move wa_itab1 to wa_itab2.

v_rkg001 = v_rkg001 + t_itab-wkg001.

v_rkg002 = v_rkg002 + t_itab-wkg002.

v_rkg003 = v_rkg003 + t_itab-wkg003.

at end of kstar.

t_itab1-objnr = wa_itab2-objnr.

t_itab1-wrttp = wa_itab2-wrttp.

t_itab1-kstar = wa_itab2-kstar.

t_itab1-wrttp = wa_itab2-wrttp.

t_itab1-wkg001 = v_rkg001.

t_itab1-wkg002 = v_rkg002.

t_itab1-wkg003 = v_rkg003.

append t_itab1.

endat.

endloop.

refresh t_itab.

t_itab[] = t_itab1[].

  • Code ends here

Cheers,

Vikram

Pls reward for helpful replies!!