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

Quantity calculation not exceeding a value

Former Member
0 Likes
2,763

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

1 ACCEPTED SOLUTION
Read only

VenkatRamesh_V
Active Contributor
0 Likes
2,683

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.

18 REPLIES 18
Read only

BH2408
Active Contributor
0 Likes
2,683

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. 

Read only

anshu_lilhori
Active Contributor
0 Likes
2,683

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

Read only

naveenvishal
Contributor
0 Likes
2,683

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

Read only

VenkatRamesh_V
Active Contributor
0 Likes
2,684

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.

Read only

Former Member
0 Likes
2,683

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

Read only

0 Likes
2,683

You mean you need to check if quantity = 4/3/5 for same combo before inserting it into new table?

Read only

0 Likes
2,683

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

Read only

0 Likes
2,683

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

Read only

0 Likes
2,683

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!

Read only

0 Likes
2,683

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.

}

Read only

0 Likes
2,683

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

Read only

0 Likes
2,683

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

Read only

0 Likes
2,683

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

Read only

former_member730258
Participant
0 Likes
2,683

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.

Read only

0 Likes
2,683

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.

Read only

0 Likes
2,683

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

Read only

0 Likes
2,683

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 .

Read only

0 Likes
2,683

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.