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.. statement

Former Member
0 Likes
925

Hi Folks,

A transparent table contains 10000 student’s name and marks in it. Please write only one SELECT statement to pick top 10 students (having maximum marks).

Its an urgent requirement.

Thanks in Advance,

A.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
893

Hi Avinash,

Hope the following logic helps you.



data : int_table  type table of table_name .
 
<b>
select * from table_name up to 10 rows 
         into table int_table 
         order by field_name descending. 
</b>

Table_name and the field_name has to be adjusted ,accordingto your requirement.

Regards,

Anirban.

8 REPLIES 8
Read only

Former Member
0 Likes
893

databse will be sorted by the key fields.

Hence select up to 10 records would give you the first 10 records(Sorted in ascending order by the key fields).

EX----

TABLES:MARA.
DATA: BEGIN OF ITAB OCCURS 0,
  MATNR LIKE MARA-MATNR,
  END OF ITAB.
  DATA: BEGIN OF ITAB1 OCCURS 0,
  MATNR LIKE MARA-MATNR,
  END OF ITAB1.


SELECT MATNR FROM MARA INTO TABLE ITAB1 .
SORT ITAB1 DESCENDING BY MATNR.

SELECT MATNR FROM MARA INTO TABLE ITAB UP TO 10 ROWS FOR  ALL ENTRIES IN ITAB1
                                                   WHERE MATNR = ITAB1-MATNR.
LOOP AT ITAB.
WRITE:/ ITAB-MATNR.
ENDLOOP.

Read only

Former Member
0 Likes
893

hi Avinash,

write a select statement which selects all the entries of students with their marks in to one internal table....

then now sort that internal table in descending order using sort statment...

loop at that itab and collect the first 10 entries of the same

Read only

former_member186741
Active Contributor
0 Likes
893

the requirement specifies to use ONE SELECT so I think you need something like:

data tm type table of mara with header

select * from mara up to 10 rows

into corresponding fields of table tm

order by brgew descending.

loop at tm.

write:/ tm-brgew.

endloop.

Obviously you will have to change it to use the 'student' table and the marks field but the technique is identical.

Read only

Former Member
0 Likes
893

Hi

Kindly try this code:

SELECT marks FROM dbTable into Internal_Table.

SORT Internal_Table BY marks DESCENDING.

LOOP AT Internal_Table.
...
...
   IF SY-TABIX EQ 11.
      EXIT.
   ENDIF.
ENDLOOP.

Best regards,

Thangesh

Read only

Former Member
0 Likes
893

Hi Avinash,

Use a <i><b>Select Query</b></i> and fill the internal table with all the records, then use,

<i><b>SORT ITAB BY MARKS DESCENDING</b></i>

and use

<i><b>LOOP AT ITAB FROM 1 TO 10.

WRITE statement.

ENDLOOP.</b></i>.

Regards:-

Santosh.

Read only

Former Member
0 Likes
893

Hi Avinash,

You can try this code:

select * from <database table> up to 10 rows

into corresponding fields of table <internaltable>

order by <field for marks> descending.

Reagrds

amit

Read only

Former Member
0 Likes
893

Hi,

Use this query :

Select Marks

into table it_marks

up to 10 rows

from ZMARKS

order by Marks descending.

Best regards,

Prashant

Read only

Former Member
0 Likes
894

Hi Avinash,

Hope the following logic helps you.



data : int_table  type table of table_name .
 
<b>
select * from table_name up to 10 rows 
         into table int_table 
         order by field_name descending. 
</b>

Table_name and the field_name has to be adjusted ,accordingto your requirement.

Regards,

Anirban.