Application Development 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: 

reade table sy-index 1

former_member586174
Participant
0 Kudos
558

Hi expert I have requirement to read the last sales amount for each MTNR

matnr    werks     Sales Priode

mat1     unit1      100    2018

mat2      unit1     200    2018

mat1      unit1     300    2017

I am trying to do

loop at lt_final into ls_final.
read lt_final into ls_final_1 index 1.
insert ls_final_1 into table lt_final_1.
endloop.

but it is not working.....:(

could someone guide to achieve this

Best Regards

Jenie

1 ACCEPTED SOLUTION

Florian
Active Contributor
411

sort it , delete the duplicates by matnr and here you are...

10 REPLIES 10

Florian
Active Contributor
412

sort it , delete the duplicates by matnr and here you are...

0 Kudos
411

This is exactly what I will do, but one a little more question, if I want to select the last three records and not just the last one, What should be done in this case..?

Florian
Active Contributor
0 Kudos
411

Get familiar with lines( itab ) and afterwards loop at index....

former_member186746
Active Contributor
411

Hi,

You're looping at a table with X amounts of records, which means the statement between loop and endloop will be executed X amount of times.

Then you read the first entry and insert that in another table X amount of times.

I suggest to position the cursor on every statement, press F1, read what it says. Then go to ABAPDOCU or DWDM transaction and spend a couple of days to get acquainted with the programming language.

Then rethink what you are trying to achieve and how the programming language can be used in getting your result.

Kind regards, Rob Dielemans

0 Kudos
411

thank you for your input

rgore
Product and Topic Expert
Product and Topic Expert
411

Hi,

Take a look at LOOP AT GROUP BY statement, refer link

Thanks & Regards,

Rakshith Gore

0 Kudos
411

thank you for your input

DoanManhQuynh
Active Contributor
411

LOOP AT...GROUP BY with key is MATNR then you have group for each MATNR. loop through group and using nmax function to get the biggest amount; or without loop just get all data of group into internal table, sort it and get the first one. just about 5 lines of code.

0 Kudos
411

thank you for your input

0 Kudos
411

Use like below Code .

sort lt_final by matnr & Date .

loop at lt_final into ls_final.

at new matnr . " After sort with date & material fist row is latest row . you can read first of every materiel row from use at new .

appen ls_final into table lt_final_1.

endat .

endloop.