‎2014 Jun 26 11:10 AM
Hi experts,
I would like to know if it’s easy to do the following with abap.
Let’s say I have the following table:
Material code | Material name | Quantity |
A | AA | 2 |
A | AA | 2 |
A | AA | 1 |
A | AC | 1 |
B | BB | 3 |
B | BB | 2 |
B | BD | 6 |
I would like to recalculate the content of this table in a new one but with the quantity has to be 4 for the same key (material code+Material name).
If it’s not possible to have 4, then 3 or 5 could be accepted.
Which gives a final table as following:
Material code | Material name | Quantity |
A | AA | 4 |
B | BB | 5 |
I tought about sorting my table by Material code, Material name and quantity.
Check if the value of the firest row is 4 or 3 or 5
if not calculate the sum of the 2 firsts rows etc
Thanks.
Amine
‎2014 Jun 26 11:37 AM
Hi Amine,
Sort the Internal table by matnr.
data: lv_tot type p decimal 0.
at new of matnr.
clear lv_tot.
endat.
lv_tot = lv_tot + itab-menge.
at end of matnr.
append lv_tot to final_itab.
endat.
Regards,
Venkat.
‎2014 Jun 26 11:21 AM
Hi Amine,
We have a control break statements in the SAP ABAP.
Please use the On change of Material Number and SUM inside a Loop.
BR,
Bharani.
‎2014 Jun 26 11:21 AM
Amine,Is this requirement related to BW ? If yes then i think just by creating material and material name as keyfield and quantity as data field you can achieve it.
You need to change the aggregation behavior of Quantity keyfigure to summation instead of overwrite in transformation.
Regards,
AL
‎2014 Jun 26 11:26 AM
Hi Amine,
You have to use Collect Statement for the same as Collect statement sums up numeric data taking all character/non-numeric fields in internal table as the key.
Hope this helps.
Regards,
Naveen
‎2014 Jun 26 11:37 AM
Hi Amine,
Sort the Internal table by matnr.
data: lv_tot type p decimal 0.
at new of matnr.
clear lv_tot.
endat.
lv_tot = lv_tot + itab-menge.
at end of matnr.
append lv_tot to final_itab.
endat.
Regards,
Venkat.
‎2014 Jun 26 12:55 PM
Hi guys,
Thanks for your answers, but how can i mange the thing of sum = 4 or 5 or 3?
I have to check multiple rows and do many tests to be sure that the quantity is equal to 4, if not 3 or 5.
Thanks.
Amine
‎2014 Jun 26 1:09 PM
You mean you need to check if quantity = 4/3/5 for same combo before inserting it into new table?
‎2014 Jun 26 1:17 PM
Hi Sameena,
Exactly, as in my example (focus only on material A):
Material code | Material name | Quantity |
A | AA | 2 |
A | AA | 2 |
A | AA | 1 |
A | AC | 1 |
B | BB | 3 |
B | BB | 2 |
B | BD | 6 |
I can also have combinations like this:
Material code | Material name | Quantity |
A | AA | 0 |
A | AA | 1 |
A | AA | 1 |
A | AA | 2 |
A | AA | 1 |
A | AC | 1 |
B | BB | 3 |
B | BB | 2 |
B | BD | 6 |
So the expected table:
Material code | Material name | Quantity |
A | AA | 4 |
B | BB | 5 |
It's not esay to explain
Thanks.
Amine
‎2014 Jun 26 1:18 PM
The idea is to check if the rows for the same key can give a sum of 4.
If not possible then 3 or 5.
If not delete.
Amine
‎2014 Jun 26 1:24 PM
1) Get all the unique combinations in some temporary table.
2) Now loop the temporary table and for each unique combination, loop your main table for the same combination key and add the quantities in a variable. If after the looping, it sum ups to your desired number, add it to the new table.
Hope it is clear!
‎2014 Jun 26 1:40 PM
have look on my approach,
1.sort main table
2.copy main table into temporary table.
3.delete adjust duplicates from temporary table.
4.create one variable for example add = 0 .
4.loop (temporary table)
{
add = 0.
loop(main table) where matrial_code = temporarytable-material code and matrial_name = temporarytable-material_name
{
if (wa_maintable-quantity = 3 or 4 or 5)
add = wa_maintable-quantity.
break.
elseif (wa_maintable-quantity = 1 or 2).
add = add + wa_maintable-quantity.
else.
break.
check add value like
if add >= 3 or add <= 5.
break.
endif.
}
append material code and name with quantity as add in new table.
}
‎2014 Jun 26 2:06 PM
Hi Sanddep,
Thanks for you answer, i dont understand this part:
elseif (wa_maintable-quantity = 1 or 2).
So i will loop to see if the sum can be 4 (4 is mu first and prefered option)
If this condition cannot be filled, so i will look for 3 or 5.
Thanks.
Amine
‎2014 Jun 26 2:14 PM
temp table[] = main table[]
Sort temp table with material code and name
delete adjacent duplicates from temp table comparing code name
DATA: l_exit type c.
Loop temporary table.
sum = 0.
clear l_exit
Loop main table where code = temporary-code
and name = temporray-name
sum = sum + quantity.
if sum > 5.
l_exit = 'X'.
exit.
endloop.
if l_exit = 'X'.
continue.
endif.
if sum = 4 or 3 or 5
append to new table.
endif.
endloop.
Message was edited by: Sameena Patel Added code to exit if sum > 5
‎2014 Jun 27 6:27 AM
Amine.
Also u need to take a flag. and set when addition is 3 or 4 or 5.
after the internal loop check flag is set or not.
If set then append new table.
else go to next record.
Regards,
Sandeep
‎2014 Jun 27 10:38 AM
loop at itab1 into wa.
collect wa into itab2.
endloop.
delete itab2 where qty LT 3 OR qty GT 5. -->itab2 will now just have all the records you need.
‎2014 Jun 27 11:07 AM
Jovee,
If you use collect without any condition that time,
adding same material code and name related quantity and create one record ,
after that you checking quantity is LT 3 or GT 5
but some case its ll not work,
like M code M name quantity
x y 2
x y 2
x y 2
p q 1
after collect
x y 6
p q 1
as per delete condition u get empty table.
but as per scenario,
output ll get
x y 4.
so u cant use collect statement without any condition.
‎2014 Jun 27 11:54 AM
Sandeep,
Jovee is right. The requirement is to exclude combinations which do not sum up to 3/4/5
You provided examples does not sum to required total hence will not be added as per requirement
‎2014 Jun 27 12:06 PM
Sameena,
As per Amine example ,
if you use Jovee method then output ll get -->
after collect,
A AA 5
A AC 1
B BB 5
B BD 6
delete LT 3 and GT 5.
A AA 5
B BB 5
Am I rght?
but amine required output
A AA 4
B BB 5
so tell me without any condition how u achieve result .
‎2014 Jun 28 4:34 AM
data l_sum type I.
sort itab1 by code.
loop at itab1 into wa.
at new code.
clear l_sum.
endat.
l_sum = l_sum + wa-qty.
if l_sum LE 5.
collect wa into itab2.
endif.
endloop.
delete itab2 where qty LT 3. -->itab2 will now just have all the records you need.
credit points if helpful. thanks.