1. ...
HAVING cond
2. ... HAVING (source_text)
Used with a SELECT statement
to be able to apply the logical condition to the groups defined in the GROUP-BY clause.
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.
... HAVING cond
Selects only those lines in the group that fulfill the HAVING condition cond.
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.
... HAVING (source_text)
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.
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.
You can mix static parts (variant 1) and dynamic parts (variant 2) as required in the HAVING clause.
Selecting Line Groups