Working with Aggregate Functions for ABAP

Applies to:
SAP ECC 6.0 (Release 700, SP 12). For more information, visit the ABAP homepage.

Summary
This article explains how to use aggregate functions in ABAP. Author: Rahul Babukuttan

Company: Applexus Software Solutions Ltd. Created on: 11 November 2010

Author Bio
Rahul Babukuttan, Applexus Software Solutions Ltd. B.Tech Computer Science Graduate, working as SAP Technology Consultant.

SAP COMMUNITY NETWORK © 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 1

................................uac.......com 2 ................................................................. 4 Advantages of using aggregate functions .....................com | BPX .................................................. 9 SAP COMMUNITY NETWORK © 2010 SAP AG SDN .................................. 7 Disadvantages of using aggregate functions ..................................................... 8 Disclaimer and Liability Notice ............................SUM ...........sap....................................bpx..................................sap........................................................................................................sdn.................... 7 Related Content ......................................................................sap......................................................................boc..................................................................................... 3 Average .....................................................................Working with Aggregate Functions for ABAP Table of Contents What are Aggregate Functions? .....................com | UAC ...............sap.............................................................................................................................................................. 3 Minimum .......................................MAX .............. 3 Maximum ......................................................................................................com | BOC ........................................................................ 4 Total ........AVG ........................................................................................ 4 Count – COUNT .................MIN .......

WRITE: /'Average value of Net-value is :'. DATA: lv_avgnetwr TYPE f. The result is a NULL value only if all the rows in the column in question contain the NULL value. Retaining duplicates is important property while computing an average. Minimum .MIN Returns the smallest value in the column determined by the database field for the selected lines. Otherwise NULL values are ignored. The result is a NULL value only if all the rows in the column in question contain the NULL value. IF sy-subrc EQ 0. ENDIF.sap. The different aggregate functions are      Average Minimum Maximum Total Count : : : : : AVG MIN MAX SUM COUNT Average .sap. Specifying DISTINCT does not change the result. IF sy-subrc EQ 0. ENDIF. PARAMETERS: p_vbeln TYPE vbeln_va. we use the keyword DISTINCT. SELECT MIN( kwmeng ) FROM vbap INTO lv_minkwmeng WHERE vbeln EQ p_vbeln. DATA: lv_minkwmeng TYPE kwmeng. The value is calculated in the database system.bpx.uac. WRITE: /'Minimum Cumulative Order Quantity is :'.com | BOC .boc. If we want to eliminate duplicates. The data type of aggregate expressions with the MIN is the data type of the corresponding column in the ABAP Dictionary. Aggregate expressions with the function AVG have the data type FLTP.sdn.com | BPX .AVG Returns the average value in the column determined by the database field for the selected lines. The specification of a column in a database table as an argument for an aggregate function constitutes an aggregate expression. SAP COMMUNITY NETWORK © 2010 SAP AG SDN .sap. which can be used in the SELECT statement. PARAMETERS: p_vbeln TYPE vbeln_va. Otherwise NULL values are ignored. lv_avgnetwr. SELECT AVG( netwr ) FROM vbap INTO lv_avgnetwr WHERE vbeln EQ p_vbeln. lv_minkwmeng.sap.Working with Aggregate Functions for ABAP What are Aggregate Functions? Aggregate functions are functions that return a single value from the values in several lines in a column in a database table.com | UAC .com 3 . AVG can only apply to a numeric field.

The result is a NULL value only if all the rows in the column in question contain the NULL value. Aggregate expressions with the function COUNT have the data type INT4.sdn. If you specify aggregate functions together with one or more database fields in a SELECT clause. IF sy-subrc EQ 0. SUM can only apply to a numeric field.com | BOC . SELECT COUNT( DISTINCT matnr ) FROM vbap INTO lv_countmatnr WHERE vbeln EQ p_vbeln.com | UAC . WRITE: /'Sum of Net-Value is:'.sap.bpx. Total . Note: Pooled and Cluster tables do not any support aggregate functions other than COUNT (*).MAX Returns the greatest value in the column determined by the database field for the selected lines. For the aggregate function SUM. make the data type of the target field longer than the data type of the source field. lv_maxkwmeng.Working with Aggregate Functions for ABAP Maximum . to allow for overflow. COUNT ( * ) (or count(*)) determines the number of rows in the resulting set or in the current group.SUM Returns the sum of all values in the column determined by the database field for the selected lines. lv_countmatnr. lv_sumkwmeng. Otherwise NULL values are ignored. In this case. ENDIF. PARAMETERS: p_vbeln TYPE vbeln_va. Otherwise NULL values are ignored. SAP COMMUNITY NETWORK © 2010 SAP AG SDN . DATA: lv_countmatnr TYPE int4. DATA: lv_sumkwmeng TYPE p decimals 2. WRITE: /'Maximum Cumulative Order Quantity is :'.boc.sap. IF sy-subrc EQ 0. Count – COUNT COUNT DISTINCT returns the number of different values in the column determined by the database field for the selected lines. In this case. NULL values are ignored unless all values in a column are NULL values.com | BPX .sap. IF sy-subrc EQ 0.uac. The result is a NULL value only if all the rows in the column in question contain the NULL value.sap. The data type of aggregate expressions with the MAX is the data type of the corresponding column in the ABAP Dictionary. The data type of the column has to be numerical. ENDIF. all database fields not used in one of the aggregate functions must be listed in the GROUP-BY clause. the result is 0. PARAMETERS: p_vbeln TYPE vbeln_va. SELECT SUM( kwmeng ) FROM vbap INTO lv_sumkwmeng WHERE vbeln EQ p_vbeln. Columns of the type STRING or RAWSTRING cannot be used with aggregate functions.com 4 . SELECT MAX( kwmeng ) FROM vbap INTO lv_maxkwmeng WHERE vbeln EQ p_vbeln. PARAMETERS: p_vbeln TYPE vbeln_va. No column label is specified in this case. DATA: lv_maxkwmeng TYPE kwmeng. the result is NULL. WRITE: /'Total No of Distinct Matrials :'. ENDIF. Specifying DISTINCT does not change the result.

bpx.boc. lv_avgnetwr. START-OF-SELECTION. " FETCH_AVGNETWR *&---------------------------------------------------------------------* *& Form FETCH_MAXKWMENG *&---------------------------------------------------------------------* * "find maximum cumulative order quantity *----------------------------------------------------------------------* FORM fetch_maxkwmeng . SELECT AVG( netwr ) FROM vbap INTO lv_avgnetwr WHERE vbeln EQ p_vbeln.sap.sap. PARAMETERS: p_vbeln TYPE vbeln_va MATCHCODE OBJECT zp_vbeln.com | BPX ."find total no of records for selection fetch_maxkwmeng. ENDFORM. PERFORM PERFORM PERFORM PERFORM PERFORM PERFORM fetch_counttotal. DATA: lv_maxkwmeng TYPE kwmeng. ENDIF. x_vbap TYPE vbap. *&---------------------------------------------------------------------* *& Form FETCH_AVGNETWR *&---------------------------------------------------------------------* * "find average Netvalue *----------------------------------------------------------------------* FORM fetch_avgnetwr .uac.com | UAC . "find average Netvalue fetch_countmatnr.Working with Aggregate Functions for ABAP In Reports the use of aggregate functions can be illustrated using the following Abap code. SELECTION-SCREEN END OF BLOCK b1. DATA: t_vbap TYPE STANDARD TABLE OF vbap."count distinct materials fetch_sumkwmeng."find minimum cumulative order quantity fetch_avgnetwr.sdn.com | BOC .sap.com 5 . IF sy-subrc EQ 0. WRITE: /'Average value of Net-value is :'.sap. *&---------------------------------------------------------------------* *& Report Z_AGGREGATE *& *&---------------------------------------------------------------------* *& *& *& *&---------------------------------------------------------------------* REPORT z_aggregate. SELECT MAX( kwmeng ) FROM vbap SAP COMMUNITY NETWORK © 2010 SAP AG SDN ."find sum of net value END-OF-SELECTION. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000. DATA: lv_avgnetwr TYPE f."find maximum cumulative order quantity fetch_minkwmeng.

sdn. ENDIF. lv_counttotal. DATA: lv_counttotal TYPE i. WRITE: /'Maximum Cumulative Order Quantity is :'. lv_minkwmeng. ENDIF.com | BOC .uac. ENDFORM. IF sy-subrc EQ 0. DATA: lv_countmatnr TYPE int4.com | UAC . SELECT COUNT( * ) FROM vbap INTO lv_counttotal WHERE vbeln EQ p_vbeln. lv_maxkwmeng.com 6 . SELECT MIN( kwmeng ) FROM vbap INTO lv_minkwmeng WHERE vbeln EQ p_vbeln. WRITE: /'Minimum Cumulative Order Quantity is :'. IF sy-subrc EQ 0.sap.boc. SELECT COUNT( DISTINCT matnr ) FROM vbap INTO lv_countmatnr WHERE vbeln EQ p_vbeln. " FETCH_MAXKWMENG *&---------------------------------------------------------------------* *& Form FETCH_MINKWMENG *&---------------------------------------------------------------------* * "find minimum cumulative order quantity *----------------------------------------------------------------------* FORM fetch_minkwmeng .com | BPX . WRITE: /'Total No of Distinct Matrials :'. ENDIF. SAP COMMUNITY NETWORK © 2010 SAP AG SDN .Working with Aggregate Functions for ABAP INTO lv_maxkwmeng WHERE vbeln EQ p_vbeln. IF sy-subrc EQ 0. WRITE: /'Total No of records for selection criteria :'. IF sy-subrc EQ 0.sap. ENDFORM. " FETCH_COUNTTOTAL *&---------------------------------------------------------------------* *& Form FETCH_COUNTMATNR *&---------------------------------------------------------------------* * "count distinct materials *----------------------------------------------------------------------* FORM fetch_countmatnr . " FETCH_MINKWMENG *&---------------------------------------------------------------------* *& Form FETCH_COUNTTOTAL *&---------------------------------------------------------------------* * "find total no of records for selection *----------------------------------------------------------------------* FORM fetch_counttotal . ENDFORM. DATA: lv_minkwmeng TYPE kwmeng.sap.bpx. lv_countmatnr.sap.

AVG.sap. etc.sap. we can group together fields to give a total value for each grouped field. Application-server and Database load is also considerably less while using aggregate functions. Performing these functions programmatically would required transferring too much data from the database to the application. Aggregate functions can also work across multiple fields. ENDIF. IF sy-subrc EQ 0.com | UAC . lv_sumkwmeng.bpx.sap. endform. Overall Network.boc.com | BPX . DATA: lv_sumkwmeng TYPE p DECIMALS 2. Aggregate functions (SUM.com 7 .sdn. MIN.sap. SAP COMMUNITY NETWORK © 2010 SAP AG SDN . Disadvantages of using aggregate functions ABAP statements that contain aggregate functions bypass R/3 table buffers. This can be a disadvantage if tables are buffered.com | BOC . Aggregate functions limit the amount of data transferred between the database and the application. Columns of the type STRING or RAWSTRING cannot be used with aggregate functions. MAX. " FETCH_COUNTMATNR *&---------------------------------------------------------------------* *& Form FETCH_SUMKWMENG *&---------------------------------------------------------------------* * "find sum of net value *----------------------------------------------------------------------* form FETCH_SUMKWMENG .Working with Aggregate Functions for ABAP ENDIF. " FETCH_SUMKWMENG Advantages of using aggregate functions Using aggregate functions significantly reduces the quantity of data records to be transferred.uac.) are the preferred method for performing mathematical and statistical functions on database records. Pooled and Cluster tables do not any support aggregate functions other than COUNT (*). WRITE: /'Sum of Net-Value is :'. SELECT sum( kwmeng ) FROM vbap INTO lv_sumkwmeng WHERE vbeln EQ p_vbeln. ENDFORM. By adding a GROUP BY clause at the end of our select statement.

sap.com 8 .bpx.com/2008/06/aggregate-functions-usage-in-abap.com | UAC .html For more information.sap.com | BOC .com/saphelp_nw04/helpdata/en/fc/eb3990358411d1829f0000e829fbfe/content.Working with Aggregate Functions for ABAP Related Content http://help.uac.sap.sap.sap.sdn.htm http://abapreports.com | BPX .blogspot.com/abapdocu_70/en/ABAPSELECT_AGGREGATE. visit the ABAP homepage.htm http://help.boc. SAP COMMUNITY NETWORK © 2010 SAP AG SDN .sap.

sap.Working with Aggregate Functions for ABAP Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP.sap. including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP.com | UAC .bpx. SAP COMMUNITY NETWORK © 2010 SAP AG SDN . SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample. or seek to hold.boc.sap.com | BPX .com 9 . SAP responsible or liable with respect to the content of this document. Changes made based on this information are not supported and can be overwritten during an upgrade. You agree that you will not hold.sdn. code or methods suggested in this document. and anyone using these methods does so at his/her own risk.sap.uac. SAP will not be held liable for any damages caused by using or misusing the information.com | BOC .

Sign up to vote on this title
UsefulNot useful