02-06-2014 8:57 AM
HI,
collect statement is used to compress unique data sets, and when it comes to amount fields it sums up the values,but i have to bifurcate records in internal table (i mean to separate records ) while using collect statement , whenever two different amount values are present for same field.
am i supposed to use different data types or different statement for the filed so that it should not collect .
please check below screen shot for exact explanation.
Example :
code work unit Quantity Rate
200021 shuttering m2 100 150
200021 shuttering m2 100 120
200022 PCC m3 90 200
200022 PCC m3 90 200
since last two records contained same value it has been compressed , whereas first two records contains same values except rate value, so i needed to show output in the below manner ,how it can be acheived.
Final output should be in this manner ..
200021 shuttering m2 100 150
200021 shuttering m2 100 120
200022 PCC m3 180 400
Thanks & Regards
J.Prakash Rao
02-06-2014 9:08 AM
Hi,
did you try to change the data type of the RATE field ? to set it in TYPE C ?
regards
Fred
02-06-2014 9:08 AM
Hi,
did you try to change the data type of the RATE field ? to set it in TYPE C ?
regards
Fred
02-08-2014 1:27 AM
HI Fredric,
ya tried but its leading to dumps, as unable to convert data types.
02-06-2014 9:15 AM
You want to club lines on the combination of Code,Work, Unit and Rate. For that you have to ensure that all these fields are characters.
Thanks
Abhinab
02-08-2014 1:29 AM
Hi abinav,
does changing data types solve my prob, but its leading to dumps as it unable to convert amount fields into char.
Thanks & REgrds.
02-06-2014 9:18 AM
is your final output
200021 shuttering m2 100 150
200021 shuttering m2 100 120
200022 PCC m3 180 400
or
200021 shuttering m2 100 150
200021 shuttering m2 100 120
200022 PCC m3 180 200
02-06-2014 9:28 AM
Hi,
try this in filling the itab:
before append/collect in itab read itab with the all fields.
if sy-subrc <> 0 use append else use collect.
Hope it helps.
Regards, Dieter
02-06-2014 9:31 AM
Hi Prakash,
DATA: lf_index TYPE sy-tabix,
lwa_final LIKE wa_final.
LOOP at i_final INTO wa_final.
*-Get the next record
lf_index = sy-tabix + 1.
READ table I_final INTO lwa_final with iINDEX lf_index.
IF sy-subrc IS INITIAL AND wa_final-code = lwa_final-code
AND wa_final-work = lwa_final-work
AND wa_final-unit = lwa_final-unit
AND wa_final-Quantity = lwa_final-Quantity
AND wa_final-Rate = lwa_final-Rate .
wa_final-rate = wa_final-rate + lwa_final-rate .
*-Append the record into new internal table and disply it
APPEND wa_final TO it_final.
ENDIF.
ENDLOOP.
SORT the internal table.
This will solve your issue.
Cheers,
Pravin
02-06-2014 9:35 AM
02-06-2014 9:43 AM
Hi Prakash Rao,
Please paste the below code , it will work according to your criteria...
TYPES : BEGIN OF ty_emp,
code TYPE char20,
work TYPE char20,
unit TYPE char2,
quantity TYPE i,
rate TYPE char3,
END OF ty_emp.
DATA : it_emp TYPE STANDARD TABLE OF ty_emp ,
wa_emp TYPE ty_emp.
START-OF-SELECTION.
BREAK-POINT.
wa_emp-code = '200021'.
wa_emp-work = 'shuttering '.
wa_emp-unit = 'm2'.
wa_emp-quantity = '100'.
wa_emp-rate = '150'.
COLLECT wa_emp INTO it_emp.
wa_emp-code = '200021'.
wa_emp-work = 'shuttering '.
wa_emp-unit = 'm2'.
wa_emp-quantity = '100'.
wa_emp-rate = '120'.
COLLECT wa_emp INTO it_emp.
wa_emp-code = '200022'.
wa_emp-work = 'pcc '.
wa_emp-unit = 'm3'.
wa_emp-quantity = '90'.
wa_emp-rate = '200'.
COLLECT wa_emp INTO it_emp.
wa_emp-code = '200022'.
wa_emp-work = 'pcc '.
wa_emp-unit = 'm3'.
wa_emp-quantity = '90'.
wa_emp-rate = '200'.
COLLECT wa_emp INTO it_emp.
In the It_emp which will have...
200021 | shuttering | m2 | 100 | 150 |
200021 | shuttering | m2 | 100 | 120 |
200022 | pcc | m3 | 180 | 200 |
Thanks ,
Vijay SR
02-06-2014 9:59 AM
Hi,
hope the below logic works for you.
1) Create an 2 new internal ITAB2 & ITAB3, pass all the values from ITAB1 to ITAB2.
2) Sort ITAB2 by Code Work Unit Quantity Rate.
3) Delete adjacent duplicate from ITAB2 comparing all the fields.
4) Loop at ITAB2 into wa_itab2.
Loop at ITAB1 into wa_itab1 where Code = wa_itab2-code
and Work = wa_itab2-work
and unit = wa_itab2-unit
and quantity = wa_itab2-quantity
and rate = wa_itab2-rate.
wa_itab3-code = wa_itab1-code.
wa_itab3-work = wa_itab1-work.
wa_itab3-unit = wa_itab1-unit.
wa_itab3-quantity = wa_itab3-quantity + wa_itab1-quantity.
wa_itab3-rate = wa_itab3-quantity + wa_itab1-rate.
clear wa_itab1.
endloop.
if not wa_itab3 is initial.
append wa_itab3 to itab3.
clear wa_itab3.
endif
clear wa_itab2.
endloop.
02-06-2014 10:07 AM
You could either
Regards,
Raymond
02-06-2014 10:44 AM
Assume you have value in itab1, Create another inter table of same type.. itab2
& also create two work area wa1 and wa2 of same type.
LOOP AT itab1 INTO wa1.
LOOP AT itab2 INTO wa2 WHERE cocode = wa1-cocode AND
work = wa1-work AND
unit = wa1-unit.
wa2-quantity = wa2-quantity + wa1-quantity.
wa2-rate = wa2-rate + wa1-rate.
MODIFY itab2 FROM wa2.
ENDLOOP.
IF sy-subrc NE 0.
APPEND wa1 TO itab2.
ENDIF.
ENDLOOP.
Now check itab2 for the required o/p.
02-08-2014 6:46 AM
FORM test_07 .
TYPES: BEGIN OF tp_sbook_1 .
TYPES: carrid TYPE sbook-carrid ,
luggweight TYPE sbook-luggweight ,
luggweighc TYPE string ,
luggweighs TYPE p DECIMALS 4 ,
count TYPE i .
TYPES: END OF tp_sbook_1 .
DATA: it_sbook TYPE TABLE OF sbook .
SELECT * INTO TABLE it_sbook
FROM sbook .
FIELD-SYMBOLS: <st_sbook> LIKE LINE OF it_sbook .
DATA: it_sbook_1 TYPE TABLE OF tp_sbook_1 .
DATA: st_sbook_1 LIKE LINE OF it_sbook_1 .
LOOP AT it_sbook ASSIGNING <st_sbook> .
MOVE-CORRESPONDING <st_sbook> TO st_sbook_1 .
st_sbook_1-luggweighc = st_sbook_1-luggweighs = <st_sbook>-luggweight .
st_sbook_1-count = 1 .
COLLECT st_sbook_1 INTO it_sbook_1 .
ENDLOOP .
FIELD-SYMBOLS: <st_sbook_1> LIKE LINE OF it_sbook_1 .
LOOP AT it_sbook_1 ASSIGNING <st_sbook_1> .
<st_sbook_1>-luggweight = <st_sbook_1>-luggweight / <st_sbook_1>-count .
ENDLOOP.
BREAK-POINT .
ENDFORM.
look at it_sbook_1 .
regards.
02-08-2014 9:44 AM
Hi Prakash,
Please check out the below code and execute it.
TYPES: BEGIN OF TY_CODE,
CODE TYPE C LENGTH 22,
WORK TYPE C LENGTH 22,
UNIT(2) TYPE C,
QUAN TYPE I,
RATE TYPE I,
END OF TY_CODE.
DATA : WA_FINAL1 TYPE TY_CODE,
IT_FINAL1 TYPE TABLE OF TY_CODE,
IT_FINAL2 LIKE IT_FINAL1,
WA_FINAL2 LIKE WA_FINAL1,
IT_FINAL3 LIKE IT_FINAL1,
WA_FINAL3 LIKE WA_FINAL1.
WA_FINAL1-CODE = '200021'.
WA_FINAL1-WORK = 'SHUTTERING'.
WA_FINAL1-UNIT = 'M2'.
WA_FINAL1-QUAN = '100'.
WA_FINAL1-RATE = '150'.
APPEND WA_FINAL1 TO IT_FINAL1.
CLEAR WA_FINAL1.
WA_FINAL1-CODE = '200021'.
WA_FINAL1-WORK = 'SHUTTERING'.
WA_FINAL1-UNIT = 'M2'.
WA_FINAL1-QUAN = '100'.
WA_FINAL1-RATE = '120'.
APPEND WA_FINAL1 TO IT_FINAL1.
CLEAR WA_FINAL1.
WA_FINAL1-CODE = '200022'.
WA_FINAL1-WORK = 'PCC'.
WA_FINAL1-UNIT = 'M3'.
WA_FINAL1-QUAN = '90'.
WA_FINAL1-RATE = '200'.
APPEND WA_FINAL1 TO IT_FINAL1.
CLEAR WA_FINAL1.
WA_FINAL1-CODE = '200022'.
WA_FINAL1-WORK = 'PCC'.
WA_FINAL1-UNIT = 'M3'.
WA_FINAL1-QUAN = '90'.
WA_FINAL1-RATE = '200'.
APPEND WA_FINAL1 TO IT_FINAL1.
CLEAR WA_FINAL1.
LOOP AT IT_FINAL1 INTO WA_FINAL1.
MOVE-CORRESPONDING WA_FINAL1 TO WA_FINAL2.
APPEND WA_FINAL2 TO IT_FINAL2.
ENDLOOP.
SORT IT_FINAL2 BY CODE WORK UNIT QUAN RATE.
DELETE ADJACENT DUPLICATES FROM IT_FINAL2 COMPARING ALL FIELDS.
LOOP AT IT_FINAL2 INTO WA_FINAL2.
LOOP AT IT_FINAL1 INTO WA_FINAL1 WHERE CODE = WA_FINAL2-CODE
AND WORK = WA_FINAL2-WORK
AND UNIT = WA_FINAL2-UNIT
AND QUAN = WA_FINAL2-QUAN
AND RATE = WA_FINAL2-RATE.
WA_FINAL3-CODE = WA_FINAL1-CODE.
WA_FINAL3-WORK = WA_FINAL1-WORK.
WA_FINAL3-UNIT = WA_FINAL1-UNIT.
WA_FINAL3-QUAN = WA_FINAL3-QUAN + WA_FINAL1-QUAN.
WA_FINAL3-RATE = WA_FINAL3-RATE + WA_FINAL1-RATE.
CLEAR WA_FINAL1.
ENDLOOP.
IF NOT WA_FINAL3 IS INITIAL.
APPEND WA_FINAL3 TO IT_FINAL3.
CLEAR WA_FINAL3.
ENDIF.
CLEAR WA_FINAL2.
ENDLOOP.
LOOP AT IT_FINAL3 INTO WA_FINAL3.
WRITE : / WA_FINAL3-CODE,WA_FINAL3-WORK,WA_FINAL3-UNIT,
WA_FINAL3-QUAN,WA_FINAL3-RATE.
ENDLOOP.
Regards,
Lavanya
02-08-2014 7:48 PM
Hi,
The collect statement sums up all numeric fields based on the character-type key fields.
BR,
Nathan
02-08-2014 9:52 PM
You can use the below code. I checked and got the output as per your requirement.
read table itab into wa index 1.
loop at itab into wa1 from 2.
if wa1 <> wa.
append wa to it_final.
else.
wa-quantity = wa-quantity + wa-quantity.
wa-rate = wa-rate + wa-rate. "Probably rate will be constant If required do it
append wa to it_final.
endif.
wa = wa1.
endloop.
Thanks,
Venkadesh