KS-06 Sorting Internal Tables

download KS-06 Sorting Internal Tables

of 19

Transcript of KS-06 Sorting Internal Tables

  • 8/11/2019 KS-06 Sorting Internal Tables

    1/19

  • 8/11/2019 KS-06 Sorting Internal Tables

    2/19

    SORTING

    INTERNAL TABLE CAN BE SORTED BY USING THESE

    ADDITIONS :

    1>.ASCENDING

    2>.DESCENDING

    3>.AS TEXT

    4>.STABLE

  • 8/11/2019 KS-06 Sorting Internal Tables

    3/19

    SORTING -..ASCENDING

    Effect

    Sorts in ascending order. This is also the default if no sort order isspecified directly after SORT. For this reason, it is not necessary to

    specifyASCENDINGexplicitly as the default sort order.

    With the addition BY, you can also specifyASCENDINGdirectlyafter a sort field to define ascending order explicitly as the sort

    sequence for this field.

  • 8/11/2019 KS-06 Sorting Internal Tables

    4/19

    EXAMPLES ARE :

    1> SORT I_ZTABLE1 .

    2.SORT I_ZTABLE1 BY NAME.

    3>SORT I_ZTABLE1 ASCENDING BY NAME.

  • 8/11/2019 KS-06 Sorting Internal Tables

    5/19

    SORTING..DESCENDING

    Effect

    Sorts in descending order. If the addition comes rightafter SORT, DESCENDINGis taken as the default for all

    fields of the sort key.

    With the addition BY, you can also specifyDESCENDINGdirectly after a sort field.

  • 8/11/2019 KS-06 Sorting Internal Tables

    6/19

    EXAMLES ARE :

    1>SORT I_ZTABLE1 DESCENDING.

    2>SORT I_ZRTABLE DESCENDING BY EMPID.

    3>SORT I_ZTABLE1 DESCENDING BY NAME.

    4>SORT I_ZTABLE1 DESCENDING BY NAME

    COUNTRY SALARY.

  • 8/11/2019 KS-06 Sorting Internal Tables

    7/19

    OTHER EXAMPLES :

    WE CAN ALSO USE THE ASCENDING AND

    DESCENDING COMBINATION LIKE :

    SORT [ASCENDENDING/DESCENDING]

    BY [ASCENDING/DESCENDING]

    [ASCENDING/DESCENDING].

    SORT I_ZTABELE1 DESCENDING BY NAME ASCENDING

    SALARY DESCNDING

    JOINING_DATE DESCENDING.

  • 8/11/2019 KS-06 Sorting Internal Tables

    8/19

    The table is now sorted by the specified

    components ... instead of by the table key. Thenumber of sort fields is limited to

    250. The sort order depends on the sequence of the fields .

    The sort sequence specified before BY applies to all fields. The sort

    sequence after a field applies only to that column of the table.You can specify a sort field dynamically by specifying () insteadof . The contents of the field determines the name of the

    sort field. If is empty when the statement is executed, the field

    is ignored in the sort. If it contains an invalid component name, a

    runtime error occurs.

  • 8/11/2019 KS-06 Sorting Internal Tables

    9/19

    SORTING..STABLE

    The option

    SORT ... STABLE.

    allows you to perform a stable sort, that is, the

    relative sequence of lines that are unchanged by the

    sort is not changed. If you do not use the STABLE

    option, the sort sequence is not preserved. If yousort a table several times by the same key, the

    sequence of the table entries will change in each

    sort. However, a stable sort takes longer than an

    unstable sort.

  • 8/11/2019 KS-06 Sorting Internal Tables

    10/19

    SORTING ALPHABETICALLY

    As well as the ASCENDING or DESCENDING addition,

    you can also specify that the entire sort or each sort field

    should be alphabetical.

    SORT ... AS TEXT ... .

    This addition affects the sort method for strings.

    Without the addition, strings are sorted according tothe sequence specified by the hardware platform.

    With the option AS TEXT, the system sorts character

    fields alphabetically according to the current text

    environment

  • 8/11/2019 KS-06 Sorting Internal Tables

    11/19

    By default, the text environment is set in the user master

    record. However, you can also set it specifically using the

    statement

    SET LOCALE LANGUAGE

    With the addition BY, you can also specifyAS TEXTdirectly aftera sort field, provided it is of type CorW, or a structured type.

    Otherwise, a runtime error occurs. In sort fields with a structuredtype,AS TEXTonly affects sub components withtype CorW.

  • 8/11/2019 KS-06 Sorting Internal Tables

    12/19

  • 8/11/2019 KS-06 Sorting Internal Tables

    13/19

  • 8/11/2019 KS-06 Sorting Internal Tables

    14/19

    The AT statements are :

    1.AT FIRST/ENDAT.

    First line of the internal table.2.ATLAST/ENDAT.

    Last line of the internal table.

  • 8/11/2019 KS-06 Sorting Internal Tables

    15/19

    3.ATNEW /ENDAT.

    Beginning of a group of lines with the same

    contents in the field and in the fields left of .

    4.ATEND Of /ENDAT.

    End of a group of lines with the same contents in

    the field and in the fields left of .

  • 8/11/2019 KS-06 Sorting Internal Tables

    16/19

    You can use control level statements to react to control

    breaks in internal tables instead of programming them

    yourself with logical expressions. Within the loop, youmust order the AT-ENDAT statement blocks according

    to the hierarchy of the control levels. :

    LOOP AT .

    AT FIRST. ... ENDAT.AT NEW . ...... ENDAT.

    AT NEW . ...... ENDAT.

    .......

    .......

    AT END OF . ... ENDAT.

    AT END OF . ... ENDAT.

    AT LAST. .... ENDAT.

    ENDLOOP.

  • 8/11/2019 KS-06 Sorting Internal Tables

    17/19

    If you are working with a work area , it does notcontain

    the current line in the AT... ENDAT statement block. All

    character fields to the right of the current group key are filled

    with asterisks (*). All other fields to the right of the current

    group key contain their initial value.

    Within an AT...ENDAT block, you can calculate the contents ofthe numeric fields of the corresponding control level using the

    SUM statement.

    SUM.

  • 8/11/2019 KS-06 Sorting Internal Tables

    18/19

    You can only use this statement within a LOOP. If you use

    SUM in an AT - ENDAT block, the system calculates

    totals for the numeric fields of alllines in the current linegroup and writes them to the corresponding fields in the

    work area (see example in ). If you use the SUM

    statement outside an AT - ENDAT block (single entry

    processing), the system calculates totals for the numericfields of alllines of the internal table in eachloop pass

    and writes them to the corresponding fields of the work

    area. It therefore only makes sense to use the SUM

    statement in AT...ENDAT blocks.

    If the table contains a nested table, you cannot use the

    SUM statement. Neither can you use it if you are using a

    field symbol instead of a work area in the LOOP

    statement.

  • 8/11/2019 KS-06 Sorting Internal Tables

    19/19

    THANK YOU.