2014 May 14 12:32 PM
Hello to all!
In sap I have a table, there are rows with the same name but with different quantity. I want to summarize this rows like this:
SELECT
c~aufnr
p~matnr p~bdter p~meins p~baugr p~dbskz p~erfmg p~aufnr
f~maktx
INTO CORRESPONDING FIELDS OF TABLE it_tab
FROM afpo AS c
INNER JOIN resb AS p ON c~aufnr = p~aufnr
INNER JOIN makt AS f ON p~matnr = f~matnr.
loop at it_tab into fs_tab.
collect fs_tab into it_tab_collected.
endloop.
it_tab = it_tab_collected.
But in this case it sums only absolutelly identically rows. I need to sum up rows only with the same name.
How can I achieve this?
Regards, Alexander.
Hello to all!
In sap I have a table, there are rows with the same name but with different quantity. I want to summarize this rows like this:
SELECT
c~aufnr
p~matnr p~bdter p~meins p~baugr p~dbskz p~erfmg p~aufnr
f~maktx
INTO CORRESPONDING FIELDS OF TABLE it_tab
FROM afpo AS c
INNER JOIN resb AS p ON c~aufnr = p~aufnr
INNER JOIN makt AS f ON p~matnr = f~matnr.
loop at it_tab into fs_tab.
collect fs_tab into it_tab_collected.
endloop.
it_tab = it_tab_collected.
But in this case it sums only absolutelly identically rows. I need to sum up rows only with the same name.
How can I achieve this?
Regards, Alexander.
2014 May 14 12:54 PM
2014 May 14 1:03 PM
Hi,
The meaning of collect statement is.. it inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key.
if want to add fields with same name then use
ADD-CORRESPONDING struc1 TO struc2.
All components of the same name in struc1 and struct2 are added in pairs and the result is assigned to the respective component of struct2.
Regards
tejas
2014 May 14 1:05 PM
i dont think that the collect statement will check the name
of the fields before summing up. it will sum the numeric fields in the given itab only if the itab and wa has the same type and considers the keys in case of sorted and hashed table(it will sum only the non unique keys if the unique keys have the same values)
which field field are you trying to summarize here ERFMG?
2014 May 14 1:20 PM
Thats right I try to sum ERFMG. So in to my table i can select only numeric fields?
2014 May 14 1:24 PM
hi,
Yes, you can sum up only numeric fields on the basis of all common character fields
Regards
Tejas
2014 May 14 1:29 PM
the collect statement will sum only the numeric fields, i think aufnr and matnr will make a unique key in your table, if right, declare it_tab_collected as sorted table with unique key aufnr matnr. then use collect statement.
i think that should work.
2014 May 15 1:12 PM
Agree with Syed. U need to check the Aufnr and Matnr to be same. Let us know
2014 May 14 1:24 PM
Hi Alex,
Yes. Using collect will add up only the numeric fields in the workarea whose key fields matches with the row already in the internal table.
Regards,
Abijith
2014 May 15 1:47 PM
Hello Alexander,
I think you should read the following guideline on COLLECT - ABAP Keyword Documentation.
If you think about the semantics of the COLLECT statement, you'll also agree that it should be used on HASHED tables(ideally) or SORTED tables(with unique primary key).
BR,
Suhas
2014 May 15 1:56 PM
Hi Alex,
Please refer below code....
DATA: BEGIN OF seats,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
seatsocc TYPE sflight-seatsocc,
END OF seats.
DATA seats_tab LIKE HASHED TABLE OF seats
WITH UNIQUE KEY carrid connid.
SELECT carrid connid seatsocc
FROM sflight
INTO seats.
COLLECT seats INTO seats_tab.
ENDSELECT.
Regards,
Thangam.P
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |