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

SAP ABAP: Internal table query

Former Member
0 Likes
1,833

Hello experts,

I am new to SAP ABAP. Could you please help me with the below issue.

I have an internal table with non-unique values as shown below.

I need to calculate the minimum price and percentage change for each material and populate the next 2 columns, which should look as below.

I tried implementing this with a select query, but it did not work. Is there any other way to modify an internal table to accommodate the changes.

Thanks in advance.

Thank you,
Abhishek

Hello experts,

I am new to SAP ABAP. Could you please help me with the below issue.

I have an internal table with non-unique values as shown below.

I need to calculate the minimum price and percentage change for each material and populate the next 2 columns, which should look as below.

I tried implementing this with a select query, but it did not work. Is there any other way to modify an internal table to accommodate the changes.

Thanks in advance.

Thank you,
Abhishek

5 REPLIES 5
Read only

0 Likes
1,611

Hi,

first of all you need to calculate the minimum for each material in your itab. To do this you need to create a temp itab which you will collect each material with the minimum price. Sample code below

types: begin of t_min,

matnr type matnr,

min type netpr,

end of t_min.

data: lt_min type table of t_min.

data: ls-min type t_min.

data: w_index type i.

loop at itab into ls_itab. "itab is you internal table.

read table lt_min into ls_min with key matnr = ls_itab-matnr.

w_index = sy-tabix

if sy-subrc <> 0. " first time

ls_min-matnr = ls_itab-matnr.

ls_min-min = ls_min-netpr.

append ls_min to lt_min.

else.

if ls_itab-netpr < ls_min-netpr.

ls_min-netpr = ls_itab-netpr.

modify lt_min from ls_min index w_index.

endif.

endif.

endloop.

After you have calculate the min for all materials just loop once more to update you original table

loop at itab into ls_itab.

w_index = sy-tabix.

read table lt_min into ls_min with key matnr = ls_itab-matnr.

ls_itab-min_price = ls_min-min.

*Now calculate persentage

ls_itab-perc = (( ls_itab-netpr - ls_itab-min_price) * 100 ) / ls_itab-min_price.

modify itab from ls_itab index w_index.

endloop.

I hope this helps.

S.Socratous

Read only

0 Likes
1,611

It would be simpler and more effective with LOOP AT ASSIGNING instead of MODIFY.

It would be contemporary with expressions.

It would be more readable if you formatted your code example.

Read only

omer_sakar
Active Participant
0 Likes
1,611

Hi,

Use control level statements like this reference : https://wiki.scn.sap.com/wiki/display/ABAP/Control+Level+Statements+in+ABAP

1. Sort itab comparing matnr netpr (descending)

2. Loop itab.

at end of matnr.

--> this line minimum of netpr

endat.

endloop.

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,611

Better use LOOP AT GROUP BY.