HAVING-Clause

Variants:




1. ... HAVING cond

2. ... HAVING (source_text)

Effect

Used with a SELECT statement to be able to apply the logical condition to the groups defined in the GROUP-BY clause.

Notes

Note

Performance:

If aggregates and groups are built by the database system and not by the application server, this may considerably reduce the amount of data to be transferred from the database server to the application server.

Variant 1

... HAVING cond


Effect

Selects only those lines in the group that fulfill the HAVING condition cond.

Example

Output the number of passengers and the average baggage weight of all Lufthansa flights on 02/28/2001 where the average baggage weight is greater than 20 kg:

DATA:  count  TYPE I,

       avg    TYPE F,
       connid TYPE sbook-connid.

SELECT CONNID COUNT( * ) avg( LUGGWEIGHT )
       INTO (connid, count, avg)
       FROM sbook
       WHERE
         carrid   = 'LH'       AND
         fldate   = '20010228'
       GROUP BY connid
       HAVING AVG( luggweight ) > '20.0'.
  WRITE: / connid, count, avg.
ENDSELECT.

Variant 2

... HAVING (source_text)


Effect

Selects only those lines from the group for which the dynamic logical condition is fulfilled which is contained in the variable source_text as ABAP source text.

Example

After 'cityfrom' and 'sbook' are assigned to the parameters comp and tabname, all departure cities with more than 3 destinations are output:

PARAMETERS: comp(80)    TYPE C,
            tabname(80) TYPE C.
DATA:   dref      TYPE REF TO DATA,
        long_name TYPE STRING,
        count     TYPE I,
        fieldlist TYPE STRING,
        condition TYPE STRING.
FIELD-SYMBOLS: <fs>.

CONCATENATE tabname '-' comp INTO long_name.
CREATE DATA dref TYPE (long_name).
ASSIGN dref->* TO <fs>.

CONCATENATE comp ' count(*)' INTO fieldlist.
condition = 'count(*) > 3'.

SELECT DISTINCT (fieldlist)
       INTO (<fs>, count)
       FROM (tabname)
       GROUP BY (comp)
       HAVING (condition).
  WRITE: / <fs>, count.
ENDSELECT.

Note

You can mix static parts (variant 1) and dynamic parts (variant 2) as required in the HAVING clause.

Additional help

Selecting Line Groups