Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Re: group by

Former Member
0 Likes
740

Hi,

when do we use group by and having clause in select stmt

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
710

The addition HAVING limits the nunber of lines to be grouped into groups in the resulting set by a logical expression sql_cond for these lines. The syntax of the logical expression sql_cond corresponds to the syntax of the logical expression sql_cond of the WHERE condition. The comparisons of the logical expression evaluate the contents of line groups.

If a grouping is done using the addition GROUP BY, all the columns that are specified in the condition sql_cond directly through their name col will be listed after GROUP BY. The direct specification of different columns leads to an exception CX_SY_OPEN_SQL_DB that can be handled. For any columns in the data base tables or Views listed after FROM, arbitrary 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 line group defined in GROUP BY and its result is used as an operand in the comparison. If such a column is also listed simultaneously 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 resulting set is grouped into a line - that is, if after SELECT you have solely aggregate expressions. In this case, solely aggregate expressions can be specified as operands in sql_cond. These operands are evaluated for all lines in the resulting set.

Example

Reading 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.

The addition HAVING limits the nunber of lines to be grouped into groups in the resulting set by a logical expression sql_cond for these lines. The syntax of the logical expression sql_cond corresponds to the syntax of the logical expression sql_cond of the WHERE condition. The comparisons of the logical expression evaluate the contents of line groups.

If a grouping is done using the addition GROUP BY, all the columns that are specified in the condition sql_cond directly through their name col will be listed after GROUP BY. The direct specification of different columns leads to an exception CX_SY_OPEN_SQL_DB that can be handled. For any columns in the data base tables or Views listed after FROM, arbitrary 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 line group defined in GROUP BY and its result is used as an operand in the comparison. If such a column is also listed simultaneously 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 resulting set is grouped into a line - that is, if after SELECT you have solely aggregate expressions. In this case, solely aggregate expressions can be specified as operands in sql_cond. These operands are evaluated for all lines in the resulting set.

Example

Reading 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.

4 REPLIES 4
Read only

Former Member
0 Likes
711

The addition HAVING limits the nunber of lines to be grouped into groups in the resulting set by a logical expression sql_cond for these lines. The syntax of the logical expression sql_cond corresponds to the syntax of the logical expression sql_cond of the WHERE condition. The comparisons of the logical expression evaluate the contents of line groups.

If a grouping is done using the addition GROUP BY, all the columns that are specified in the condition sql_cond directly through their name col will be listed after GROUP BY. The direct specification of different columns leads to an exception CX_SY_OPEN_SQL_DB that can be handled. For any columns in the data base tables or Views listed after FROM, arbitrary 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 line group defined in GROUP BY and its result is used as an operand in the comparison. If such a column is also listed simultaneously 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 resulting set is grouped into a line - that is, if after SELECT you have solely aggregate expressions. In this case, solely aggregate expressions can be specified as operands in sql_cond. These operands are evaluated for all lines in the resulting set.

Example

Reading 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.

Read only

Former Member
0 Likes
710

Hi

Though they are SQL related commands we rarely use them in ABAP select statements

we fetch the data into Internal table and then will use the

SORT command to sort that itab by the required fields which serves the same purpose similar to GROUP By

Regards

Anji

Read only

Former Member
0 Likes
710

HI,

The addition GROUP BY combines groups of rows that have the same content in their specified columns (col1 col2 ...) in the resulting set into a single row.

rgds,

bharat.

Read only

Former Member
0 Likes
710

Hi,,

When we want to get the records ordered by some fields we use the clause "group by " in select statement.

But it s always better to sort the internal table after selection rather than using group by