The only limitation with SAP open SQL is that you can't
sort DATABASE result onto an aggregate field (SUM( ), COUNT( ), AVG( )...).
Otherwise, using the UP TO argument you can limit the flow between database
& application server but this is not useful in your case because you cannot
sort the result at the database level.
Following eg list the top 20 semi-finished material ordered by gross weight.
PARAMETERS P_DATE LIKE VBAP-ERDAT.
TABLES: VBAP, MARA.
DATA: BEGIN OF ITAB OCCURS 0,
MATNR LIKE MARA-MATNR,
COUNT TYPE I,
BRGEW LIKE VBDKA-BRGEW,
END OF ITAB.
SELECT VBAP-MATNR COUNT( * ) SUM( VBAP-BRGEW ) " UP TO 20 ROWS
INTO TABLE ITAB
FROM ( VBAP
INNER JOIN MARA
ON MARA-MATNR EQ VBAP~MATNR )
WHERE MARA-MTART EQ 'HALB'
AND VBAP-ERDAT GT P_DATE
GROUP BY VBAP-MATNR.
* ORDER BY xxx.
SORT ITAB BY BRGEW DESCENDING.
LOOP AT ITAB.
CHECK SY-TABIX LT 20.
WRITE: / ITAB-MATNR, ITAB-COUNT, ITAB-BRGEW.
ENDLOOP.