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

Select Distinct/MAX

0 Likes
2,336

Hello,

I have the following entries in MARC (example)

MaterialPlantProfit Center
123456US500000
123456MX500000
123456TJ600000
123456AU500000
123456FR500000
7890US600000
7890MX600000
7890AU500000
7890FR600000

I would like to do something like the following:

SELECT DISTINCT on material number with the MAXIMUM number of profit centers

So I would end up with the following entries in my internal table:

123456  500000

7890     600000

Unfortunately, I cannot restrict based on plant because there isn't one plant that all Materials exist in.

I understand I can select all records and delete duplicates but this does not guarantee that I will get the profit center which appears the most.

Thank you!

4 REPLIES 4
Read only

former_member183045
Contributor
0 Likes
1,257

I would suggest first to make an ordered group by:

SELECT material, profit_center, count( * )  as amount

FROM MARC

INTO CORRESPONDING FIELDS OF lv_table

GROUP BY material, profit_center

ORDER BY material, amount descending.

Then loop through the result table lv_table and write the first element of each material (eg. by writing them into a new table)

Read only

0 Likes
1,257

Not DISTINCT but GROUP BY, did you try a simple:


SELECT matnr MAX( prctr ) INTO CORRESPONDING FIELDS OF marc FROM marc GROUP BY matnr.

Hint: Read online documention of SELECT statement for aggregate.

Regards,

Raymond

Read only

VenkatRamesh_V
Active Contributor
0 Likes
1,257

Hi  jess,

Try,

Sort  internal table by material profit center descending.

using the read statement with  material, you can get desired result.

Hope it helpful,

Regards,

Venkat.

Read only

roberto_forti
Contributor
0 Likes
1,257

Hi Jess,

About performance you can sort by material profit descending and read with material to get the first material record .

Regards