1. ...
ORDER BY PRIMARY KEY
2.
... ORDER BY f1 ... fn
3. ... ORDER BY (source_text)
Orders the records in a SELECT
statement. Without the ORDER-BY clause, the order in which
the selected lines are supplied is undefined. This means that two similar SELECT statements may produce lines in a different order.
...ORDER BY PRIMARY KEY
Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
Output the passenger list for the Lufthansa flight 0400 on 28.02.2001:
DATA: wa_sbook TYPE sbook.
SELECT * FROM sbook INTO wa_sbook
WHERE
carrid = 'LH ' AND
connid = '0400'
AND
fldate = '20010228'
ORDER BY PRIMARY KEY.
WRITE: /
wa_sbook-bookid, wa_sbook-customid,
wa_sbook-custtype, wa_sbook-smoker,
wa_sbook-luggweight, wa_sbook-wunit,
wa_sbook-invoice.
ENDSELECT.
Since views do not have a primary key, specifying ORDER BY PRIMARY
KEY
only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
ORDER BY f1 ... fn
Sorts the selected records in ascending order by the specified column
references f1 ... fn. If a list is also specified
in the SELECT clause,
the column references f1,
..., fn
must appear in this list.
By supplementing the statement with DESCENDING, you
can sort in descending order using any of the fields f1, ..., fn.
The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
Output Lufthansa flights from 27.02.2001 to 05.03.2001, sorted by plane type and number of occupied seats:
DATA: wa_sflight TYPE sflight.
SELECT * FROM sflight INTO wa_sflight
WHERE carrid = 'LH' AND
fldate BETWEEN '20010227' AND '20010305'
ORDER
BY planetype ASCENDING seatsocc DESCENDING.
WRITE: / wa_sflight-planetype, wa_sflight-seatsocc,
wa_sflight-connid, wa_sflight-fldate.
ENDSELECT.
Performance:
... ORDER BY (source_text)
Works like
ORDER BY f1 ... fn if the variable source_text
contains the list f1 ... fn as ABAP source text.
The same restrictions apply to this variant as to ORDER BY f1 ... fn.
After you enter 'cityfrom' or 'cityto', the system outputs all departure or destination cities of Lufthansa, including the number of destinations.
PARAMETERS: comp(80).
DATA: dref TYPE REF TO DATA,
long_name
TYPE STRING,
name TYPE STRING,
ftab
TYPE TABLE OF STRING,
count TYPE I.
FIELD-SYMBOLS:
<fs>.
name = 'SPFLI'.
CONCATENATE name '-' comp INTO long_name.
CREATE DATA dref
TYPE (long_name).
ASSIGN dref->* TO <fs>.
APPEND comp TO ftab.
APPEND 'COUNT(
* ) AS COUNT' TO ftab.
SELECT DISTINCT (ftab)
INTO
(<fs>, count)
FROM (name)
WHERE
carrid = 'LH'
GROUP BY (comp)
ORDER BY (comp).
WRITE: / <fs>, count.
ENDSELECT.
Specifying a Sort Sequence