Thursday 4 January 2018

Maximum, Minimum, Average, Sum and Count by Select (ABAP SELECT AGGREGATE Statement)

Aggregate Statement return a single value from the values in several lines of a column in  a database table.
The value is calculated  in the database system.
These Statement can be used with SELECT statement by specifying a column(column Field) of a database table as an argument.
Any number of column field can be used as arguments of aggregate Statement in SELECT statement.
Also multiple aggregate Statement can be used in a single SELECT statement.

Following are the aggregate Statement available in sap abap :

1 Average(AVG)
2 MAXIMUM(MAX)
3 Minimum(MIN)
4 Total(SUM)
5 Count(COUNT) 



Average(AVG) : 
Syntax : AVG([DISTINCT]  field1) AVG determines the  average value  of the column field1 specified. It can only be applied to a numeric field.
Data types like  DF16_DEC, D34_DEC (decimal floating point numbers ) , F(float) are recommended for the target fields.


Maximum(MAX) :
Syntax : MAX([DISTINCT] field1)
MAX determines the maximum value of the contents of the column field1 specified.


Minimum(MIN) : 
Syntax : MIN([DISTINCT]  field1)
MIN determines the minimum value of the contents of the column field1 specified.


Total(SUM) :
Syntax : SUM([DISTINCT]  field1)
SUM determines the sum of contents of the column field1 specified.
This can only be applied to a numeric field
To avoid overflows it is recommended to make data type of target field greater than of the source field.


Count(COUNT) :
1. Syntax :  COUNT(DISTINCT field1)
It determines the number of different values in the column field1 .

2. Syntax :  COUNT( * )
It determines the number of rows in the result set. No column field field1 is specified in this case.


example : In the ekpo table we find MENGE field with PO number '3500014180'.




Output






Some interesting points about aggregate Statement :
1. When aggregate Statements are used,the SELECT statement bypasses SAP table buffering. It is a disadvantage if tables are buffered.
2. Columns of the type STRING or RAWSTRING cannot be used with aggregate Statement.
3. Pooled and Cluster tables do not any support aggregate Statement other than COUNT (*).
4. If the addition FOR ALL ENTRIES is used before WHERE , apart from COUNT(*) no other aggregate Statement can be used.

4 comments:

  1. I need to calculate line items of PO price, this is not working

    ReplyDelete
  2. I need to calculate line items of PO price, this is not working

    ReplyDelete
  3. Try below query.. :)

    select ebeln as po ,count(*) as line_item,sum( netwr ) from ekpo into table @data(it_ekpo) where ebeln in s_ebeln group by ebeln.

    ReplyDelete
  4. I want to create a program to find min,max and avg using subroutine or function module.can anyone pls help me out .

    ReplyDelete

In this post we use some basics Events  and Control Break Statements for Generating Classical Report Based On single Table. If You want so...