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

Retrieve value

0 Likes
2,086

Im having a little trouble building the logic.

I need to retrieve the highest concat column value per material

1 ACCEPTED SOLUTION
Read only

LaurensDeprost
Contributor
2,011

Hi Adrian,

Take a look at the documentation for the openSQL GROUP BY clause.

Adjust your original database query if possible to avoid selecting unnecessary data.
Alternatively, if your system supports it (>7.40), you can select from your internal table:

 SELECT
   FROM @original_itab AS material
   FIELDS material~matnr,
          MAX( material~concat ) AS max_concat_for_material
   GROUP BY material~matnr
   INTO TABLE @DATA(materials_with_max_concat).

Finally, another option is sorting your itab, loop over it, and extract the maximum value per material number.

8 REPLIES 8
Read only

LaurensDeprost
Contributor
0 Likes
2,011

Hi Adrian,

How exactly are you having trouble/what have you tried to achieve the desired result?
Have you tried searching the Q&A section for similar questions?

Read only

0 Likes
2,011

Yes, I already searched for some Q&A sections, I only saw on how to get the highest value on the column. But what I need is to get the highest value in concat column per material

Read only

Dominik_Tylczynski
SAP Champion
SAP Champion
0 Likes
2,011

Hello kuser0320

Just sort the table by CONCAT column descending and read the first row - see SAP Help SORT itab

Best regard

Dominik Tylczynski

Read only

2,011

Hello Dominik, I can't just sort concat column. Your solution will just give me the highest value in concat column. But what I need is the highest value for every matnr column

Read only

0 Likes
2,011
kuser0320

You are right. I somehow missed that part. Still the other answer give good solutions.

Read only

VXLozano
Active Contributor
0 Likes
2,011

If you just need to order the data:

  • when selecting the data: ORDER BY
  • after selection: SORT BY

If you need a new table with just the highest values:

  • when selecting the data: MAX( )
  • after selection: LOOP + INSERT (not sure if there are new syntax for these things, I'm sure they will be)

But if you have problem with this "logic", you need to go back to the basics of programming, I fear. No offense.

Read only

LaurensDeprost
Contributor
2,012

Hi Adrian,

Take a look at the documentation for the openSQL GROUP BY clause.

Adjust your original database query if possible to avoid selecting unnecessary data.
Alternatively, if your system supports it (>7.40), you can select from your internal table:

 SELECT
   FROM @original_itab AS material
   FIELDS material~matnr,
          MAX( material~concat ) AS max_concat_for_material
   GROUP BY material~matnr
   INTO TABLE @DATA(materials_with_max_concat).

Finally, another option is sorting your itab, loop over it, and extract the maximum value per material number.

Read only

former_member598787
Participant
0 Likes
2,011

Hello ,

Please refer to below piece of code:

Output is :

2562 20251

2562 20221

2705 20211

2705 20201

types: begin of ty_mara,
        matnr type matnr,
        concat(15) type c,
      end of ty_mara.
types: ty_t_mara type standard table of ty_mara.
data: ls_mara type ty_mara.
data : lt_mara type standard table of ty_mara.
ls_mara-matnr = '000000000000002705'.
ls_mara-concat = '20201'.
append ls_mara to lt_mara.
ls_mara-matnr = '000000000000002705'.
ls_mara-concat = '20211'.
append ls_mara to lt_mara.
ls_mara-matnr = '000000000000002562'.
ls_mara-concat = '20251'.
append ls_mara to lt_mara.
ls_mara-matnr = '000000000000002562'.
ls_mara-concat = '20221'.
append ls_mara to lt_mara.
sort lt_mara by matnr  concat descending.
clear ls_mara.
loop at lt_mara assigning field-symbol(<lfs_mara>).
    write :/ <lfs_mara>-matnr .
    write : <lfs_mara>-concat.
endloop.