SAP HAVING CLAUSE ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for HAVING_CLAUSE

SELECT - HAVING

Short Reference
• HAVING SELECT


ABAP Syntax ... HAVING sql_cond ... .


What does it do? The addition HAVING limits the number of rows in groups in the results set by using the logical expression sql_cond on these rows. The syntax of the logical expression sql_cond matches the syntax of the logical expression sql_cond of the WHERE condition. The comparisons of the logical expression evaluate the contents of row groups.

If rows are grouped using the addition GROUP BY, all the columns that are specified directly in the condition sql_cond using their name col are listed after GROUP BY. Specifying a different column directly raises the handleable exception CX_SY_OPEN_SQL_DB. For any columns in the database tables or views listed after FROM, any aggregate expressions can be specified in the listed database tables in the comparisons of sql_cond . This kind of aggregate expression is evaluated for each row group defined in GROUP BY and its result is used as an operand in the comparison. If, at the same time, a column of this type is also listed as an argument of an aggregate function after SELECT , the aggregate expressions after SELECT and after HAVING can be different.

If the addition GROUP BY is not specified or the data object column_syntax in the dynamic column specification after GROUP BY is initial, the addition HAVING can only be specified if the entire results set is grouped into a line (that is, if there are only aggregate expressions specified after SELECT). In this case, only aggregate expressions can be specified as operands in sql_cond . These operands are evaluated for all rows in the results set.



Example ABAP Coding
Reads the number of booked smoking and non-smoking seats for each flight date of a particular flight connection. PARAMETERS: p_carrid TYPE sbook-carrid,
p_connid TYPE sbook-connid.

TYPES: BEGIN OF sbook_type,
fldate TYPE sbook-fldate,
smoker TYPE sbook-smoker,
smk_cnt TYPE i,
END OF sbook_type.

DATA sbook_tab TYPE TABLE OF sbook_type.

SELECT fldate smoker COUNT( * ) AS smk_cnt
FROM sbook
INTO CORRESPONDING FIELDS OF TABLE sbook_tab
WHERE connid = p_connid
GROUP BY carrid fldate smoker
HAVING carrid = p_carrid
ORDER BY fldate smoker.

Return to menu