Collect statement in Loop..
Hi,
I have an Internal table ITAB with following records.
REC1 10
REC1 20
REC2 15
REC2 25
REC2 35
and I need an output
REC1 30
REC2 75
I tried like this
LOOP AT ITAB.
COLLECT ITAB.
MODIFY ITAB.
ENDLOOP.
Am i doing something wrong?? I am still getting 5 records in output..
regards
Praveen
Hi Praveen,
You create another internal table with te same table type.
Then you write collect on the internal table.
define Itab1 like itab. and write logic like below.
LOOP AT ITAB.
ITAB1 = ITAB.
COLLECT ITAB1.
ENDLOOP.
Thanks,
Ramakrishna
Similar Messages
-
How to use collect statement properly
In PBID table , i will get 4 rows for a material . I will get the corresponding values of bdzei from the same table . Now i will pass this bdzei into pbhi table. then i will get some 200 rows of data. I have to sum up the field plnmg , by grouping the laeda field of pbhi. In short, i need to know the sum of pbhi-plnmg for a particular pbhi-laeda . I know a way to do it. But i want to know how to use the COLLECT statement for this purpose. My output should contain oly 1 line for 1 material ..
PBID table
Matnr BDZEI
p4471 457
1002
2309
2493
PBHI table
BDZEI LAEDA PLNMG
1002 06.08.2004 0.000
1002 06.08.2004 83.000
457 07.08.2004 12.000
457 07.08.2004 24.000
Reqd O/p
MATNR LAEDA PLNMG
p4471 06.08.2004 83
p4471 07.08.2004 36
Hope u understood my situation .please help me out ...
Thanking you in advance ..
ShankarREPORT zppr_zpipr NO STANDARD PAGE HEADING LINE-SIZE 150 LINE-COUNT 63.
TABLES: pbid,
pbhi,
makt,
mseg,
mkpf.
DATA: BEGIN OF it_pbid OCCURS 0,
matnr LIKE pbid-matnr, " Material
status TYPE c LENGTH 4, " For distinguishing materials from pbid and pbim .. will contain space for PBID and 'PBIM' for PBIM
bdzei LIKE pbid-bdzei,
laeda LIKE pbhi-laeda,
end of it_pbid.
DATA: BEGIN OF it_pbim OCCURS 0,
matnr LIKE pbid-matnr, " Material
status TYPE c LENGTH 4, " For distinguishing materials from pbid and pbim .. will contain space for PBID and 'PBIM' for PBIM
bdzei LIKE pbim-bdzei,
laeda LIKE pbhi-laeda,
end of it_pbim.
DATA: BEGIN OF it_pbid_pbim OCCURS 0,
matnr LIKE pbid-matnr, " Material
laeda LIKE pbhi-laeda, " Reduction Date
dbmng LIKE pbhi-dbmng, " Planned quantity in the data base
plnmg LIKE pbhi-plnmg, " Planned quantity
status TYPE c LENGTH 4, " For distinguishing materials from pbid and pbim .. will contain space for PBID and 'PBIM' for PBIM
mblnr LIKE mseg-mblnr, " Material Doc Number
pbfnr LIKE pbid-pbdnr, " Plan Number
maktx LIKE makt-maktx, " Matl Desc
aenam LIKE pbhi-aenam, " User Changed
erfmg LIKE mseg-erfmg, " Qty Invoiced
budat LIKE mkpf-budat, " Invoice date
bdzei LIKE pbid-bdzei, " Independent requirements pointer
werks LIKE pbid-werks, " plant
pirrednqty TYPE i, " PIR Reduction Quantity = pbih-plnmg - pbih-dbmng
diff TYPE i, " Difference
slno TYPE i, " Sl No
END OF it_pbid_pbim.
DATA: BEGIN OF it_allrows OCCURS 0.
INCLUDE STRUCTURE it_pbid_pbim.
DATA: END OF it_allrows.
*DATA: BEGIN OF it_final OCCURS 0.
INCLUDE STRUCTURE it_pbid_pbim.
*DATA: END OF it_final.
DATA: BEGIN OF line,
matnr LIKE pbid-matnr, " Material
laeda LIKE pbhi-laeda, " Reduction Date
plnmg LIKE pbhi-plnmg, " Planned quantity
maktx LIKE makt-maktx, " Matl Desc
dbmng LIKE pbhi-dbmng, " Planned quantity in the data base
mblnr LIKE mseg-mblnr, " Material Doc Number
pbfnr LIKE pbid-pbdnr, " Plan Number
aenam LIKE pbhi-aenam, " User Changed
erfmg LIKE mseg-erfmg, " Qty Invoiced
budat LIKE mkpf-budat, " Invoice date
bdzei LIKE pbid-bdzei, " Independent requirements pointer
werks LIKE pbid-werks, " plant
pirrednqty TYPE i, " PIR Reduction Quantity = pbih-plnmg - pbih-dbmng
diff TYPE i, " Difference
slno TYPE i, " Sl No
status TYPE c LENGTH 4, " For distinguishing materials from pbid and pbim .. will contain space for PBID and 'PBIM' for PBIM
END OF line.
DATA Itfinal1 LIKE STANDARD TABLE
OF LINE
WITH DEFAULT KEY.
DATA ITfinal LIKE ITfinal1.
DATA: BEGIN OF it_dates OCCURS 0,
date TYPE sy-datum,
END OF it_dates.
DATA: l_slno TYPE i.
DATA: l_zebra TYPE c.
SELECT-OPTIONS:
s_werks FOR pbid-werks obligatory.
SELECT-OPTIONS:
s_matnr FOR pbid-matnr.
SELECT-OPTIONS:
s_pbdnr FOR pbid-pbdnr.
SELECT-OPTIONS:
s_laeda FOR pbhi-laeda obligatory.
parameter:
c_print type checkbox.
SELECT matnr bdzei FROM pbid INTO (it_pbid-matnr, it_pbid-bdzei) WHERE werks IN s_werks AND matnr IN s_matnr AND pbdnr IN s_pbdnr.
it_pbid-status = 'PBID'.
APPEND it_pbid.
move-corresponding it_pbid to it_pbid_pbim.
Append it_pbid_pbim.
ENDSELECT.
SELECT matnr bdzei FROM pbim INTO (it_pbim-matnr,it_pbim-bdzei) WHERE werks IN s_werks AND matnr IN s_matnr AND pbdnr IN s_pbdnr.
APPEND it_pbim.
Append it_pbid_pbim.
ENDSELECT.
*break-point.
START-OF-SELECTION.
LOOP AT s_laeda.
it_dates-date = s_laeda-low.
APPEND it_dates.
ENDLOOP.
DATA: l_startdate LIKE sy-datum.
IF s_laeda-high EQ space.
ELSE.
l_startdate = s_laeda-low + 1.
DO.
IF l_startdate <= s_laeda-high.
it_dates-date = l_startdate.
APPEND it_dates.
ELSE.
it_dates-date = sy-datum.
EXIT.
ENDIF.
l_startdate = l_startdate + 1.
ENDDO.
ENDIF.
*break-point.
LOOP AT it_pbim.
LOOP AT it_dates.
it_pbim-laeda = it_dates-date.
it_pbim-status = 'PBIM'.
MODIFY it_pbim TRANSPORTING laeda status.
MOVE-CORRESPONDING it_pbim TO it_pbid_pbim.
APPEND it_pbid_pbim.
ENDLOOP.
ENDLOOP.
break-point.
l_zebra = 'X'.
DATA: l_toterfmg LIKE mseg-erfmg.
DATA: l_erfmg LIKE mseg-erfmg.
data: l_totpir type i.
**************************************PBID*************************************
LOOP AT it_pbid.
move-corresponding it_pbid to it_pbid_pbim.
append it_pbid_pbim.
SELECT SINGLE maktx FROM makt INTO (it_pbid_pbim-maktx) WHERE matnr = it_pbid-matnr.
MODIFY table it_pbid_pbim TRANSPORTING maktx.
SELECT aenam laeda FROM pbhi INTO (it_pbid_pbim-aenam,it_pbid_pbim-laeda) WHERE bdzei = it_pbid-bdzei AND aenam = 'Abbau-'.
MODIFY TABLE it_pbid_pbim TRANSPORTING aenam laeda. " debug here
select single sum( dbmng ) sum( plnmg ) FROM pbhi INTO (it_pbid_pbim-dbmng,it_pbid_pbim-plnmg) WHERE bdzei = it_pbid-bdzei AND aenam = 'Abbau-' and laeda = it_pbid_pbim-laeda..
MODIFY TABLE it_pbid_pbim TRANSPORTING dbmng plnmg. " debug here
endselect.
it_pbid_pbim-pirrednqty = it_pbid_pbim-dbmng - it_pbid_pbim-plnmg.
MODIFY table it_pbid_pbim TRANSPORTING pirrednqty.
IF ( it_pbid_pbim-laeda IN s_laeda AND it_pbid_pbim-aenam EQ 'Abbau-' ).
MOVE-CORRESPONDING it_pbid_pbim TO it_allrows.
append it_allrows.
ELSEIF NOT it_pbid_pbim-laeda IN s_laeda.
delete it_pbid_pbim index sy-tabix. " debug here
ENDIF.
ENDSELECT.
ENDLOOP.
**************************************PBIM*************************************
LOOP AT it_pbim.
move-corresponding it_pbim to it_pbid_pbim.
append it_pbid_pbim.
SELECT SINGLE maktx FROM makt INTO (it_pbid_pbim-maktx) WHERE matnr = it_pbim-matnr.
MODIFY table it_pbid_pbim TRANSPORTING maktx.
SELECT aenam laeda FROM pbhi INTO (it_pbid_pbim-aenam,it_pbid_pbim-laeda) WHERE bdzei = it_pbim-bdzei AND aenam = 'Abbau-'.
MODIFY TABLE it_pbid_pbim TRANSPORTING aenam laeda. " debug here
select single sum( dbmng ) sum( plnmg ) FROM pbhi INTO (it_pbid_pbim-dbmng,it_pbid_pbim-plnmg) WHERE bdzei = it_pbim-bdzei AND aenam = 'Abbau-' and laeda = it_pbid_pbim-laeda..
MODIFY TABLE it_pbid_pbim TRANSPORTING dbmng plnmg. " debug here
it_pbid_pbim-pirrednqty = it_pbid_pbim-dbmng - it_pbid_pbim-plnmg.
MODIFY table it_pbid_pbim TRANSPORTING pirrednqty.
IF ( it_pbid_pbim-laeda IN s_laeda AND it_pbid_pbim-aenam EQ 'Abbau-' ).
MOVE-CORRESPONDING it_pbid_pbim TO it_allrows.
append it_allrows.
ELSEIF NOT it_pbid_pbim-laeda IN s_laeda.
delete it_pbid_pbim index sy-tabix. " debug here
ENDIF.
ENDSELECT.
ENDLOOP.
sort it_allrows by matnr laeda status.
**********************************ALL ROWS************************
loop at it_allrows.
line-matnr = it_allrows-matnr.
line-laeda = it_allrows-laeda.
line-plnmg = it_allrows-plnmg.
line-dbmng = it_allrows-dbmng.
line-mblnr = it_allrows-mblnr.
line-pbfnr = it_allrows-pbfnr.
line-maktx = it_allrows-maktx.
line-aenam = it_allrows-aenam.
line-erfmg = it_allrows-erfmg.
line-budat = it_allrows-budat.
line-bdzei = it_allrows-bdzei.
line-werks = it_allrows-werks.
line-pirrednqty = it_allrows-pirrednqty.
line-diff = it_allrows-diff.
line-slno = it_allrows-slno.
line-status = it_allrows-status.
collect line into itfinal1.
endloop.
loop at itfinal1 into line.
collect line into itfinal.
write: / line-matnr, line-plnmg, line-dbmng,line-laeda,line-status..
endloop.
skip 4.
loop at itfinal into line.
write: / line-matnr, line-plnmg, line-dbmng,line-laeda,line-status..
endloop.
break-point. -
Issue while using Collect statement in program
Hi friend's,
i have some issue regarding Collect statement..
Issue is somthing like this,i have created a structure and internal table and work area with same type,
u can look in below code..i wann to collect 'enmng' f'ield value whose matnr,charg,mvt type ,aufnr,rsnum are same but rspos and lgort are differnt for this i need to collect 'ENMNG' field value can any one tel me what to do...it not collectng value instead of this it append entire row...
types: begin of ty_resb,
rsnum type resb-rsnum, "Resrvation Number
rspos type resb-rspos, "item position
xloek type resb-xloek, "Item Deleted if = 'X'
matnr type resb-matnr, "Material Number
lgort type resb-lgort, "Storage location 'REJT'
charg type resb-charg, "Batch No
enmng type resb-enmng, "Quantity withdrawn should be greater then '0'
enwrt type resb-enwrt, "Value Withdrawn
aufnr type resb-aufnr, "Order
bwart type resb-bwart, "Movement type
end of ty_resb.
data:tt_resb type table of ty_resb ,
tt_resb1 type table of ty_resb ,
tt_resb2 type standard table of ty_resb ,
**workarea.
ts_resb type ty_resb,
ts_resb2 type ty_resb,
ts_resb1 type ty_resb.
**move data to another internal table
tt_resb1[] = tt_resb[].
sort tt_resb by rsnum rspos matnr charg bwart lgort aufnr .
sort tt_resb1 by rsnum rspos matnr charg bwart lgort aufnr.
DELETE adjacent duplicates from tt_resb comparing rsnum rspos matnr charg bwart lgort aufnr .
DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.
loop at tt_resb into ts_resb .
clear:ts_resb1,ts_resb2.
read table tt_resb1 into ts_resb1 with key rsnum = ts_resb-rsnum rspos = ts_resb-rspos matnr = ts_resb-matnr
charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.
if ts_resb1-bwart = '261' .
move ts_resb1-aufnr to ts_resb2-aufnr.
move ts_resb1-matnr to ts_resb2-matnr.
move ts_resb1-charg to ts_resb2-charg.
move ts_resb1-bwart to ts_resb2-bwart.
move ts_resb1-enwrt to ts_resb2-enwrt.
move ts_resb1-lgort to ts_resb2-lgort.
move ts_resb1-enmng to ts_resb2-enmng.
collect ts_resb2 into tt_resb2.
endif.
clear ts_resb.
endloop.
Regard's,
shaikh khalid.Hi Shaikh,
I have added new declarations as highlighted below and new lines within your loop:
Execute your program in debug mode and see what happens to the internal tt_collect I added. The collect will sum the fields enmng for all entries that have same value on the fields matnr,charg,mvt type ,aufnr,rsnum.
Question from me: what do you want to do with the collected/Summed up entries?
types: begin of ty_resb,
rsnum type resb-rsnum, "Resrvation Number
rspos type resb-rspos, "item position
xloek type resb-xloek, "Item Deleted if = 'X'
matnr type resb-matnr, "Material Number
lgort type resb-lgort, "Storage location 'REJT'
charg type resb-charg, "Batch No
enmng type resb-enmng, "Quantity withdrawn should be greater then '0'
enwrt type resb-enwrt, "Value Withdrawn
aufnr type resb-aufnr, "Order
bwart type resb-bwart, "Movement type
end of ty_resb.
data:tt_resb type table of ty_resb ,
tt_resb1 type table of ty_resb ,
tt_resb2 type standard table of ty_resb ,
types: begin of ty_collect,
rsnum type resb-rsnum,
matnr type resb-matnr,
charg type resb-charg,
bwart type resb-bwart,
aufnr type resb-aufnr,
enmng type resb-enmng,
end of ty_collect.
data: tt_collect type table of ty_collect,
ts_collect type ty_collect.
**workarea.
ts_resb type ty_resb,
ts_resb2 type ty_resb,
ts_resb1 type ty_resb.
**move data to another internal table
tt_resb1[] = tt_resb[].
sort tt_resb by rsnum rspos matnr charg bwart lgort aufnr .
sort tt_resb1 by rsnum rspos matnr charg bwart lgort aufnr.
DELETE adjacent duplicates from tt_resb comparing rsnum rspos matnr charg bwart lgort aufnr .
DELETE adjacent duplicates from tt_resb1 comparing rsnum rspos matnr charg bwart lgort aufnr.
loop at tt_resb into ts_resb .
clear:ts_resb1,ts_resb2.
read table tt_resb1 into ts_resb1 with key rsnum = ts_resb-rsnum rspos = ts_resb-rspos matnr = ts_resb-matnr
charg = ts_resb-charg aufnr = ts_resb-aufnr binary search.
if ts_resb1-bwart = '261' .
move ts_resb1-aufnr to ts_resb2-aufnr.
move ts_resb1-matnr to ts_resb2-matnr.
move ts_resb1-charg to ts_resb2-charg.
move ts_resb1-bwart to ts_resb2-bwart.
move ts_resb1-enwrt to ts_resb2-enwrt.
move ts_resb1-lgort to ts_resb2-lgort.
move ts_resb1-enmng to ts_resb2-enmng.
collect ts_resb2 into tt_resb2.
move-corresponding ts_resb1 to ts_collect.
collect ts_collect into tt_collect
endif.
clear ts_resb.
clear ts_collect.
endloop. -
Hi,
i have requirement to get all unique entries from table .if i am getting duplicate record then i need to add qunatity field which i have define like rlbes-anfme.
my internal table is :
11 name1 23.00
22 name2 1,324.00
11 name1 10.00
I want output in this way
11 name1 33.00
22 name2 1,324.00
i tried to use collect statement but the problem is its not working on character type field.
if i use packed field then i am not able to upload my file because i am getting quantity filed 1,2344.00 this way.( instead of 1325.00)
please give me any solution.
thanks,
jackHi,
Try this..
Create another internal table which has the same structure of your internal table.
DATA: LT_COLLECT LIKE ITAB OCCURS 0 WITH HEADER LINE.
DATA: V_QUANTITY TYPE MENGE_D.
Sort the internal table.
SORT ITAB BY field1 field2. " Give the appropriate field names.
<b>* process the internal table which has the records.</b>
LOOP AT ITAB.
Sum up the quantity.
V_MENGE = V_MENGE + ITAB-ANFME. " Quantity field.
Move the first two fields.
LT_COLLECT-FIELD1 = ITAB-FIELD1.
LT_COLLECT-FIELD2 = ITAB-FIELD2.
At the end of the record..append the record.
AT END OF FIELD2. " Give the corresponding field name.
LT_COLLECT-ANFME = V_MENGE.
APPEND LT_COLLECT.
CLEAR: V_MENGE.
ENDAT.
ENDLOOP.
Thanks,
Naren -
Reg: Collect statement in internal table
Hi,
Can any one tell me wether we can use the collect statement in an internal table which has no header line
if it can be used can anyone please send me a sample snippet
Thanks In ADVANCE
Guhapriyan SubrahmanyamHi Guhapriyan,
I have used the collect statement in the way as shown..
DATA : BEGIN of I_ALLOCATIONS OCCURS 0,
PSPNR LIKE PRPS-PSPNR,
PSPID LIKE PROJ-PSPID,
PSPHI LIKE PRPS-PSPHI,
POST1 LIKE PROJ-POST1,
ZZLOB LIKE PROJ-ZZLOB,
WERKS LIKE PROJ-WERKS,
SOBID LIKE HRP1001-SOBID,
OBJID LIKE HRP1001-OBJID,
BEGDA LIKE HRP1001-BEGDA,
ENDDA LIKE HRP1001-ENDDA,
PROZT LIKE HRP1001-PROZT,
LOB LIKE HRP1000-STEXT,
TEMP TYPE P decimals 2,
TYPE(3),
ZROLE LIKE PA0001-ZROLE,
HOLID TYPE P decimals 2,
LEAVE TYPE P decimals 2,
DAYS TYPE P decimals 2,
END of I_ALLOCATIONS.
DATA: I_ALLOCATION LIKE I_ALLOCATIONS OCCURS 0 WITH HEADER LINE.
************Populate I_ALLOCATIONS **************
LOOP AT I_ALLOCATIONS.
COLLECT I_ALLOCATIONS INTO I_ALLOCATION.
ENDLOOP.
Actually i had requirement like u, but this one is rather simple...
Get the required data in an I_Table which dont require Header line..
Later on collect this particular I_Table in other I_Table1 with the Header Line...
Regards,
Abhishek -
Two collect statement from internal table
Dear Experts,
I want populate values to two internal tables from a internal table.
can i use two collect statement for different internal table from a single internal table.
advise please.
Thanks in advance.
R.Rajendranhi there....
well u can very well use this thing.....
use the two colect statements inside the loop which u will use to read the records of the input table.
hope it helps.
do reward if it does. -
Hi friends i have doubt on collect statement.
i have some problem with matnr which is repeating no.of times. so i tried using collect and selected some of the fields to collect. Now i need to collect those collected elements in to internal table and need to display those fields in an ouput. for example.
loop at sumtab.
matnr type s886-matnr,
avg1 type p decimals 3,
total type p decimals 3,
endloop.
and now using loop i am collecting. these fields using my itab.
loop at itab.
move-corresponding itab to sumtab.
collect sumtab.
clear sumtab.
endloop.
***here itab contains no.of other fields like qty,stock etc.
so after here i am using ALV to display the itab.
my problem is how to make SUMTAB fields tobe moved in itab.Hi Kalyan,
The collect statement works like this . (ex)
data: begin of itab occurs 0,
name(10) type c,
value(4) type n,
end of itab.
itab-name = 'JACK'.
itab-value = 100.
append itab.
itab-name = 'PAT'.
itab-value = 200.
append itab.
itab-name = 'JACK'.
itab-value = 200.
append itab.
sort itab.
loop itab.
collect itab.
write: / itab-name, itab-value.
endloop.
<b>o/p:</b>
JACK 300
PAT 200 -
Problem with Collect statement
Hi Experts,
I am facing a peculiar problem. Please go through the code below and it is not giving the aggregate of it_ekbe_642_pgi-menge for the same it_ekbe_642_pgi-ebeln and it_ekbe_642_pgi-ebelp.
LOOP AT it_ekbe_642_pgi.
it_ekbe_642_pgi_tot-ebeln = it_ekbe_642_pgi-ebeln.
it_ekbe_642_pgi_tot-ebelp = it_ekbe_642_pgi-ebelp.
it_ekbe_642_pgi_tot-budat = it_ekbe_642_pgi-budat.
it_ekbe_642_pgi_tot-xblnr = it_ekbe_642_pgi-xblnr.
it_ekbe_642_pgi_tot-menge = it_ekbe_642_pgi-menge.
COLLECT it_ekbe_642_pgi_tot.
ENDLOOP.
Please suggest.
Thanks....
Shibaji.Hi Shibaji Maitra ,
Since you are aggregating the it_ekbe_642_pgi-menge for the same it_ekbe_642_pgi-ebeln and it_ekbe_642_pgi-ebelp.
For using the Collect statement the order of the fields should be
in proper order i.e. all the char fields come up & then after that
all the numeric fields.Please check the sequence in your internal
table and also the fields type of menge field.
LOOP AT it_ekbe_642_pgi.
it_ekbe_642_pgi_tot-ebeln = it_ekbe_642_pgi-ebeln.
it_ekbe_642_pgi_tot-ebelp = it_ekbe_642_pgi-ebelp.
it_ekbe_642_pgi_tot-xblnr = it_ekbe_642_pgi-xblnr.
it_ekbe_642_pgi_tot-budat = it_ekbe_642_pgi-budat.
it_ekbe_642_pgi_tot-menge = it_ekbe_642_pgi-menge.
COLLECT it_ekbe_642_pgi_tot.
ENDLOOP.
Rewards points if helpful.
Regards
Manoj Kumar -
Hi,
My collect statement is not working. Kindly let me know what is the other way to add one filed in the loop.
thanks
Moderator message - Please ask a specific question - post locked
Edited by: Rob Burbank on Nov 19, 2009 9:24 AMSame thing i faced few days ago --. i have resolved this using following logic ... try this it will workout
CLEAR: wa_mkpf_mseg, w_werks, w_matnr, w_lgort, w_bwart.
LOOP AT t_mkpf_mseg INTO wa_mkpf_mseg.
IF wa_mkpf_mseg-werks EQ w_werks AND wa_mkpf_mseg-matnr EQ w_matnr AND wa_mkpf_mseg-lgort EQ w_lgort AND wa_mkpf_mseg-bwart EQ w_bwart.
CLEAR: wa_p_coi.
READ TABLE t_p_coi INTO wa_p_coi WITH KEY werks = wa_mkpf_mseg-werks matnr = wa_mkpf_mseg-matnr lgort = wa_mkpf_mseg-lgort bwart = wa_mkpf_mseg-bwart.
IF sy-subrc = 0.
wa_p_coi-Avg Days = wa_p_coi-Avg Days + wa_mkpf_mseg-Avg Days.
MODIFY t_p_coi FROM wa_p_coi TRANSPORTING Avg Days .
CLEAR: wa_p_coi, wa_p_coi.
endif.
ELSE.
wa_p_coi-werks = wa_mkpf_mseg-werks.
wa_p_coi-matnr = wa_mkpf_mseg-matnr.
wa_p_coi-bwart = wa_mkpf_mseg-bwart.
wa_p_coi-lgort = wa_mkpf_mseg-lgort.
wa_p_coi-Avg Days = wa_mkpf_mseg-Avg Days .
APPEND wa_p_coi TO t_p_coi.
CLEAR: wa_p_coi.
ENDIF.
w_werks = wa_mkpf_mseg-werks.
w_matnr = wa_mkpf_mseg-matnr.
w_lgort = wa_mkpf_mseg-lgort.
w_bwart = wa_mkpf_mseg-bwart.
CLEAR: wa_mkpf_mseg.
ENDLOOP. -
Collect statement in a class.
Hello,
This is related to BW infospoke enhancement,
in this class i have structure which is filled and i have to aggregate the data in this structure by one of the field level,
i want to declare a internal table with header and take the data from structure into internal table and then do a collect statement on to this internal table..
but i am getting a message that no one more header line is supported in OOPS concept..meaning i am not able to declare a internal table with header line..
any solution to this will be awarded with points.Hello Shashi
You should not use itab with header lines since they mess up any coding and are a nightmare when it comes to maintaining code.
Fortunately, SAP has abandoned header lines from ABAP-OO. If you are not using table types then you have to define your own types, e.g.:
TYPES: BEGIN OF ty_s_line.
TYPES: field_a TYPE ... .
TYPES: field_b TYPE ... .
TYPES: END OF ty_s_line.
TYPES: ty_t_itab TYPE STANDARD TABLE OF ty_s_line
WITH DEFAULT KEY.
DATA:
ls_line TYPE ty_s_line,
lt_itab TYPE ty_t_itab.
* Now fill your itab, e.g.
SELECT * FROM ... INTO CORRESPONDING FIELDS
OF TABLE lt_itab.
* Now working with the itab data.
LOOP AT lt_itab INTO ls_line.
ENDLOOP.
Regards
Uwe -
Internal Table Line Operations - Collect Statement
Hello Gurus,
I have an internal table with data in it. The fields of the internal table are
Location - Matnr - Qty - Flag.
Same material can be at different locations with different quantities. I need to get material and total quantity of the material in all the locations in a new internal table.
If the table (it_tab) contains -
Location - matnr - qty - flag
1200 abc 10 S
1201 abc 20 S
1205 abc 30 S
1207 abc 50 S
1200 xyz 20 S
1201 xyz 25 S
1300 xyz 22 S
From this table, I need to get the result int table (it_res) as below -
Matnr - qty
abc 110
xyz 67
110 = 102030+50
67 = 252220
My idea is we can use collect statement looping the it_tab table.
Please help me how to do this.
Regards,
BaluHi balu,
1. Simple
2. Create one another internal table STAB,
with only two fields
a) MATNR
b) QTY
3. Now,
4.
Loop at Itab.
Move-corresponding itab to STAB.
COLLECT STAB.
Endloop.
regards,
amit m. -
Collect Statement to group on character fields
Hi,
I am working on some specific requirements on a support project and found a bit scenario where there is a data in an internal table and displayed the group wise total. for which the collect statement is used.
loop at lt_matdata into lwa_matdata.
lwa_final-docno = lwa_matdata-docno.
lwa_final-matnr = lwa_matdata-matnr.
lwa_final-puqty = lwa_matdata-puqty.
lwa_final-amt = lwa_matdata-amount.
collect lwa_final into lt_final.
endloop.
now the problem is i need to add RATE also to the final table. if i add the field rate to the final it will display a sum. I tried to find something that helps me adding a field RATE. Please suggest a way where i can skip the sum in collect for the specific field.
one way is i can create an another internal table and make a read statement to display the data, but wanted to know if there is any other simple and suitable solution to this.
Thanks,
JanisarHi,
Run this code & get the idea for your program...
DATA: BEGIN OF line,
col1(3) TYPE c,
col2(2) TYPE n,
col3 TYPE i,
col4 type i,
END OF line.
DATA itab LIKE SORTED TABLE OF line
WITH NON-UNIQUE KEY col1 col2.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 3.
COLLECT line INTO itab.
line-col4 = 12.
modify itab from line index sy-tabix transporting col4.
WRITE / sy-tabix.
clear line.
line-col1 = 'def'. line-col2 = '34'. line-col3 = 5.
COLLECT line INTO itab.
line-col4 = 12.
modify itab from line index sy-tabix transporting col4.
WRITE / sy-tabix.
clear line.
line-col1 = 'abc'. line-col2 = '12'. line-col3 = 7.
COLLECT line INTO itab.
line-col4 = 12.
modify itab from line index sy-tabix transporting col4.
WRITE / sy-tabix.
clear line.
LOOP AT itab INTO line.
WRITE: / line-col1, line-col2, line-col3, line-col4.
ENDLOOP.
In your code you try this,
loop at lt_matdata into lwa_matdata.
lwa_final-docno = lwa_matdata-docno.
lwa_final-matnr = lwa_matdata-matnr.
lwa_final-puqty = lwa_matdata-puqty.
lwa_final-amt = lwa_matdata-amount.
collect lwa_final into lt_final.
* ADD THESE LINES
lwa_final-rate = lwa_matdata-rate.
modify it_final from lwa_final index sy-tabix transporting rate.
clear lwa_final.
endloop. -
Regarding collect statement - Production report
Dear Experts,
I've the following requirement
A ) I'm creating a ALV report from Z table where i want the summary of the qty across different processing type like CT , ST , WS & FG .
B ) For each processing types i want the total qty to be displayed in different columns.
C ) When i'm trying to execute , no data is displaying..
Piece of coding is as below.. Request help to clear the below issue..
REPORT ZPP_PRD_REPORT.
TABLES : ZPP_ACTUAL_PRD.
TYPES : BEGIN OF TY_ACTUAL_PRD,
KDAUF TYPE KDAUF,
KDPOS TYPE KDPOS,
WERKS TYPE WERKS_D,
PROCTYP TYPE ZDE_PP_PROCTYP,
AUFNR TYPE AUFNR,
MEINS TYPE MEINS,
END OF TY_ACTUAL_PRD.
TYPES : BEGIN OF TY_FINAL,
KDAUF TYPE KDAUF,
KDPOS TYPE KDPOS,
WERKS TYPE WERKS_D,
PROCTYP TYPE ZDE_PP_PROCTYP,
AUFNR TYPE AUFNR,
MEINS TYPE MEINS,
END OF TY_FINAL.
DATA : GT_ACTUAL_PRD TYPE TABLE OF TY_ACTUAL_PRD,
GS_ACTUAL_PRD TYPE TY_ACTUAL_PRD,
GT_ACTUAL_PRD_COLLECT TYPE TABLE OF TY_ACTUAL_PRD.
DATA : GT_FINAL TYPE TABLE OF TY_FINAL,
GS_FINAL TYPE TY_FINAL.
DATA : GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
GS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA : GT_EVENTS TYPE SLIS_T_EVENT.
DATA : GD_LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA : GS_REPID LIKE SY-REPID.
SELECT-OPTIONS : S_KDAUF FOR ZPP_ACTUAL_PRD-KDAUF,
S_KDPOS FOR ZPP_ACTUAL_PRD-KDPOS.
START-OF-SELECTION.
PERFORM GET_DATA.
PERFORM POPULATE_DATA1.
*& Form GET_DATA
* text
* --> p1 text
* <-- p2 text
FORM GET_DATA .
SELECT
KDAUF
KDPOS
WERKS
PROCTYP
AUFNR
MEINS
FROM ZPP_ACTUAL_PRD INTO TABLE GT_ACTUAL_PRD WHERE KDAUF IN S_KDAUF AND
KDPOS IN S_KDPOS.
ENDFORM. " GET_DATA
*& Form POPULATE_DATA1
* text
* --> p1 text
* <-- p2 text
FORM POPULATE_DATA1 .
LOOP AT GT_ACTUAL_PRD INTO GS_ACTUAL_PRD.
GS_FINAL-KDAUF = GS_ACTUAL_PRD-KDAUF.
GS_FINAL-KDPOS = GS_ACTUAL_PRD-KDPOS.
GS_FINAL-WERKS = GS_ACTUAL_PRD-WERKS.
GS_FINAL-PROCTYP = GS_ACTUAL_PRD-PROCTYP.
GS_FINAL-AUFNR = GS_ACTUAL_PRD-AUFNR.
GS_FINAL-MEINS = GS_ACTUAL_PRD-MEINS.
COLLECT GS_ACTUAL_PRD INTO GT_ACTUAL_PRD_COLLECT.
ENDLOOP.
ENDFORM. " POPULATE_DATA1
*& Form BUILD_FIELDCATALOG
* text
* --> p1 text
* <-- p2 text
FORM BUILD_FIELDCATALOG .
GS_FIELDCAT-FIELDNAME = 'KDAUF'.
GS_FIELDCAT-SELTEXT_M = 'SALESORDER'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'KDPOS'.
GS_FIELDCAT-SELTEXT_M = 'SALESITEM'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'WERKS'.
GS_FIELDCAT-SELTEXT_M = 'PLANT'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'PROCTYP'.
GS_FIELDCAT-SELTEXT_M = 'TYPE'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'AUFNR'.
GS_FIELDCAT-SELTEXT_M = 'ORDER'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'MENGE'.
GS_FIELDCAT-SELTEXT_M = 'QTY'.
GS_FIELDCAT-OUTPUTLEN = 10.
GS_FIELDCAT-DO_SUM = 'X'.
GS_FIELDCAT-NO_ZERO = 'X'.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
ENDFORM. " BUILD_FIELDCATALOG
*& Form ALV_DISPLAY
* text
* --> p1 text
* <-- p2 text
FORM ALV_DISPLAY .
GS_REPID = SY-REPID.
IF GS_REPID IS NOT INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = GS_REPID
IT_FIELDCAT = GT_FIELDCAT[]
IT_EVENTS = GT_EVENTS[]
I_SAVE = 'A'
I_DEFAULT = 'X'
TABLES
T_OUTTAB = GT_ACTUAL_PRD_COLLECT
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.
ENDFORM. " ALV_DISPLAY
Z Data base table looks as below..
Best rgds/thnks,
Srikanth.Hi Sankara ,
Collect statement is used to add all the numeric components to the corresponding values of all the existing rows in an internal table , with the same key . The data type can be I , P , F .
Please ensure that there is a comman value in all the rows .
check this out and let me know if any issues
Regards,
Kavitha -
Hi , what is collect statement, can any one explain me with an example...
thanks in advancesay u have 10 line items and u want to have common line items as such then u use collect statement.
say, internal table has material and quantity.
aaa 10
aaa 20
bbb 5
ccc 8
bbb 3
aaa 5
bbb 7
so, if u want only rows as
aaa qty
bbb qty
ccc qty
sort itab by material.
loop at itab.
itab1-material = itab-material.
itab1-qty = itab-qty.
collect itab1.
endloop.
so, itab1 will have
aaa 35
bbb 15
ccc 8
hope u understood the concept.
Reward Points... -
Hi
can anyone give me some idea how to use collect statement with index
here in the below syntax i want to use collect instead of insert.
INSERT WA_ALVDISPLAY INTO IT_ALVDISPLAY index 3 .
thanks.
vivekkhi
my scenario is like this
i need an yearly report of a examination held in a school based on month,each month they need report for girls and boys separately
the output format should be like this
month:marks( boys) marks( girls )
so i have created 3 internal tables suppose itab ,itab1,itab2.
itab1-month1
itab1-boy_sub1
itab1-boy_sub2
itab2-month2
itab2-girl_sub1
itab2-girl_sub2
itab -month1
itab -boy_sub1
itab -boy_sub2
itab -girl_sub1
itab -girl_sub2
first i am looping in itab1 and using COLLECT statement i am inserting data to itab (bez in a month more than one exam can happen)
i got boys mark in itab,then i need to put girls mark on the same row based on month
based on particular index i need to insert data, moreover i need to use COLLECT statement for getting the total of particular month
thanks
vivek
Maybe you are looking for
-
Why am I seeing a "Download Error" message in Creative Cloud Desktop app?
am connecting with the internet and my login info. is correct but always i have a massage say download error please contact customer support . do i have to download it again ?
-
My iPhone 4 was purchased in May 2012, brand new, from my local AT&T store. Since then, I've been having minor problems with it such as my messages crashing out when I open it or my phone randomly turning off and then turning back on, but that has ne
-
Download less than three at a time
If I remember well with past ios for iphones and ipads, we were able to download one song/movie/serie etc at a time instead of three. I don't see anymore this option. Is it still possible?
-
Feedback form - specify who the sender is.
Hello, I have a dreamweaver form that allows the user to fill in contact information and comments. When the form sends, it uses the "name" field as the sender info when I check it in my email account. It works properly on my email account through my
-
Hello, I need any help I can get. I use BB Curve 8320 fron Airtel Nigeria: Issues: 1) Recently I discovered that I can no longer compose sms by following the normal route " Menu, Compose sms". When I press that nothing happens. I found that I can sti